When to make constructor explicit in C++ [closed]

The traditional wisdom is that constructors taking one parameter (explicitly or effectively through the use of default parameters) should be marked explicit, unless they do define a conversion (std::string being convertible from const char* being one example of the latter). You’ve figured out the reasons yourself, in that implicit conversions can indeed make life harder … Read more

Explicit move constructor?

An explicit move constructors can affect compatibility with e.g. Standard algorithms. For instance, std::swap<T> requires that T be MoveConstructible. In turn, MoveConstructible is specified in terms of an expression, namely T u = rv; (where rv is an rvalue of type T). If there is neither a non-explicit copy constructor nor a non-explicit move constructor … Read more

What is the difference between implicit constructors and default constructors?

The terms default and implicit, when talking about a constructor have the following meaning: default constructor is a constructor that can be called with no arguments. It either takes no arguments or has default values for each of the arguments taken. implicit constructor is a term commonly used to talk about two different concepts in … Read more

Why is explicit allowed for default constructors and constructors with 2 or more (non-default) parameters?

One reason certainly is because it doesn’t hurt. One reason where it’s needed is, if you have default arguments for the first parameter. The constructor becomes a default constructor, but can still be used as converting constructor struct A { explicit A(int = 0); // added it to a default constructor }; C++0x makes actual … Read more

What could go wrong if copy-list-initialization allowed explicit constructors?

Conceptually copy-list-initialization is the conversion of a compound value to a destination type. The paper that proposed wording and explained rationale already considered the term “copy” in “copy list initialization” unfortunate, since it doesn’t really convey the actual rationale behind it. But it is kept for compatibility with existing wording. A {10, 20} pair/tuple value … Read more

C++ always use explicit constructor [closed]

The traditional wisdom is that constructors taking one parameter (explicitly or effectively through the use of default parameters) should be marked explicit, unless they do define a conversion (std::string being convertible from const char* being one example of the latter). You’ve figured out the reasons yourself, in that implicit conversions can indeed make life harder … Read more

techhipbettruvabetnorabahisbahis forumu