Namespaces
Variants
Views
Actions

Talk:cpp/language/explicit

From cppreference.com

Contents

[edit] static_cast

It should be used either static_cast or int() in both A and B examples.
I think int() makes it more readable, is there any reason to use static_cast?
-- Bazzy 15:07, 13 March 2012 (PDT)

C-style cast is too generic to be useful (except when you're implementing std::addressof or offsetof). Especially when the purpose is to explain something. Editing to static cast --Cubbi 19:02, 13 March 2012 (PDT)

[edit] structures without member variables

Is there a reason the structs in the examples do not have member variables? Does it make it a better example? Arbalest 14:47, 14 December 2012 (PST)

It's to keep the example as minimal as possible. The definition of the structs is not really relevant.
The example is there just to show what would compile and what not.
--Bazzy 15:05, 14 December 2012 (PST)

[edit] Multiple arguments

explicit on a constructor with multiple arguments has no effect, since such constructors cannot take part in implicit conversions. However, for the purpose of implicit conversion, explicit will have an effect if a constructor has multiple arguments and all but one of the arguments has a default value.

wouldn't it be simpler to say that it has no effect on constructors that aren't converting constructors? --Cubbi (talk) 13:53, 22 September 2014 (PDT)
having looked at it, that would've been a tautology: converting constructors are defined as non-explicit constructors, regardless of the number of non-default arguments. --Cubbi (talk) 17:01, 22 September 2014 (PDT)

[edit] template constructors can be explicit too

Not sure how useful such information, but template constructors can be explicit too, e.g.

struct C
{
    template<class T>
    explicit C(T const &)
    {
    }
};

AFAIK such constructor will match any argument, e.g. C(5.0) or C(std::string()) making no sense to specify "explicit".

certainly, both constructors (other than copy/move) and user-defined conversion functions may be function templates (I added a line). This has no special effect on the meaning of explicit; an explicit template constructor will not be considered in copy-initialization, just like how an explicit non-template constructor isn't: C c = 5.0; would not compile. --Cubbi (talk) 07:42, 18 December 2014 (PST)
Oh, I should sleep more! You right, of course template or not "explicit" specifier meaning doesn't change Rutsky (talk) 12:57, 18 December 2014 (PST)

[edit] example improvement

Should we add situations in which the struct A or B is contextually-convertible-to-bool, to the example? Just like that of I/O Streams.

That to save this problem. --LittleFlower (talk) 23:36, 31 January 2018 (PST)