explicit-constructor

This is not copy-initializing, or is it?

北战南征 提交于 2019-11-29 19:52:39
问题 In the following code I am not allowed to declare an explicit ctor because the compiler says I am using it in a copy-initializing context (clang 3.3 and gcc 4.8). I try to prove the compilers wrong by making the ctor non explicit and then declaring the copy constructors as deleted. Are the compilers wrong or is there any other explanation? #include <iostream> template <typename T> struct xyz { constexpr xyz (xyz const &) = delete; constexpr xyz (xyz &&) = delete; xyz & operator = (xyz const &

What's the difference between explicit and implicit assignment in C++

我只是一个虾纸丫 提交于 2019-11-29 03:25:19
int value = 5; // this type of assignment is called an explicit assignment int value(5); // this type of assignment is called an implicit assignment What is the difference between those, if any, and in what cases do explicit and implicit assignment differ and how? http://weblogs.asp.net/kennykerr/archive/2004/08/31/Explicit-Constructors.aspx EDIT: I actually just found this article, which makes the whole thing a lot clearer... and it brings up another question, should you ( in general ) mark constructors taking a single parameter of a primitive type - numeric/bool/string - as explicit and

Purpose of Explicit Default Constructors

老子叫甜甜 提交于 2019-11-28 06:13:51
I recently noticed a class in C++0x that calls for an explicit default constructor. However, I'm failing to come up with a scenario in which a default constructor can be called implicitly. It seems like a rather pointless specifier. I thought maybe it would disallow Class c; in favor of Class c = Class(); but that does not appear to be the case. Some relevant quotes from the C++0x FCD, since it is easier for me to navigate [similar text exists in C++03, if not in the same places] 12.3.1.3 [class.conv.ctor] A default constructor may be an explicit constructor; such a constructor will be used to

When should you use direct initialization and when copy initialization?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 06:01:31
Is it simply preference or are there specific instances where one is necessary over another? I'm refering to the following variants for initialization T t(e); // direct initialization T t = e; // copy initialization The actual names of the things you describe is not implicit and explicit assignment but : Copy-initialization : T x = a; Direct-initialization : T x(a); They are not equivalent, most notably in contexts where a conversion is required, for example when T is of class type and a is of a different type (see Alf comment for examples of contexts which don't even involve conversion).

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

落花浮王杯 提交于 2019-11-27 19:37:27
In the C++ standard, §13.3.1.7 [over.match.list], the following is stated: In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed. This is the reason why we can't do, for example, something like this: struct foo { // explicit because it can be called with one argument explicit foo(std::string s, int x = 0); private: // ... }; void f(foo x); f({ "answer", 42 }); (Note that what happens here is not a conversion , and it would not be one even if the constructor was "implicit". This is initialization of a foo object using its constructor directly. Other

When should you use direct initialization and when copy initialization?

柔情痞子 提交于 2019-11-27 01:09:41
问题 Is it simply preference or are there specific instances where one is necessary over another? I'm refering to the following variants for initialization T t(e); // direct initialization T t = e; // copy initialization 回答1: The actual names of the things you describe is not implicit and explicit assignment but : Copy-initialization : T x = a; Direct-initialization : T x(a); They are not equivalent, most notably in contexts where a conversion is required, for example when T is of class type and a

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

喜夏-厌秋 提交于 2019-11-27 00:43:14
问题 I understand that constructors with one (non-default) parameter act like implicit convertors, which convert from that parameter type to the class type. However, explicit can be used to qualify any constructor, those with no parameters (default constructor) or those with 2 or more (non-default) parameters. Why is explicit allowed on these constructors? Is there any example where this is useful to prevent implicit conversion of some sort? 回答1: One reason certainly is because it doesn't hurt.

C++ deprecated conversion from string constant to &#39;char*&#39;

大城市里の小女人 提交于 2019-11-26 00:23:40
问题 I have a class with a private char str[256]; and for it I have an explicit constructor: explicit myClass(const char *func) { strcpy(str,func); } I call it as: myClass obj(\"example\"); When I compile this I get the following warning: deprecated conversion from string constant to \'char*\' Why is this happening? 回答1: This is an error message you see whenever you have a situation like the following: char* pointer_to_nonconst = "string literal"; Why? Well, C and C++ differ in the type of the

What does the explicit keyword mean?

ε祈祈猫儿з 提交于 2019-11-25 21:43:41
问题 What does the explicit keyword mean in C++? 回答1: The compiler is allowed to make one implicit conversion to resolve the parameters to a function. What this means is that the compiler can use constructors callable with a single parameter to convert from one type to another in order to get the right type for a parameter. Here's an example class with a constructor that can be used for implicit conversions: class Foo { public: // single parameter constructor, can be used as an implicit conversion