Questions tagged [explicit-constructor]

In C++ prefixing the explicit keyword to the constructor prevents the compiler from using that constructor for implicit conversions.

In C++, 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 single parameter constructors to convert from one type to another in order to get the right type for a parameter.

Prefixing the explicit keyword to the constructor prevents the compiler from using that constructor for implicit conversions.

The reason you might want to do this is to avoid accidental construction that can hide bug.

References:

68 questions
3615
votes
11 answers

What does the explicit keyword mean?

What does the explicit keyword mean in C++?
Skizz
  • 69,698
  • 10
  • 71
  • 108
177
votes
11 answers

C++ deprecated conversion from string constant to 'char*'

I have a class with a private char str[256]; and for it I have an explicit constructor: explicit myClass(char *func) { strcpy(str,func); } I call it as: myClass obj("example"); When I compile this I get the following warning: deprecated…
mkamthan
  • 2,331
  • 3
  • 17
  • 23
114
votes
4 answers

Explicit constructor taking multiple arguments

Does making a constructor having multiple arguments explicit have any (useful) effect? Example: class A { public: explicit A( int b, int c ); // does explicit have any (useful) effect? };
Peter G.
  • 14,786
  • 7
  • 57
  • 75
49
votes
2 answers

Purpose of Explicit Default Constructors

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…
Dennis Zickefoose
  • 10,791
  • 3
  • 29
  • 38
45
votes
1 answer

C++ always use explicit constructor

After reading the following blog : http://xania.org/200711/ambiguous-overloading I started asking myself "should I not always explicit define my constructors?" So I started reading more than found out this article…
oopsi
  • 1,919
  • 3
  • 21
  • 28
43
votes
4 answers

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

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…
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
39
votes
5 answers

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

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…
Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292
34
votes
1 answer

Explicit keyword on multi-arg constructor?

I recently came across some weird looking class that had three constructors: class Class { public: explicit Class(int ); Class(AnotherClass ); explicit Class(YetAnotherClass, AnotherClass ); // ... } This doesn't…
LiraNuna
  • 64,916
  • 15
  • 117
  • 140
30
votes
5 answers

Explicit move constructor?

The explicit keyword is recommended for all most constructors which can be called with one argument, except for copy constructors. For copy constructors, it has an use (to forbid implicit copying via function call, return, etc), but it's not what's…
Kos
  • 70,399
  • 25
  • 169
  • 233
21
votes
2 answers

When should you use direct initialization and when copy initialization?

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
21
votes
2 answers

If I use explicit constructor, do I need to put the keyword in both .h and .cpp files?

Actually my question is all in the title. Anyway: I have a class and I use explicit constructor: .h class MyClass { public: explicit MyClass(const string& s): query(s) {} private: string query; } Is it obligatory or not to put explicit…
chester89
  • 8,328
  • 17
  • 68
  • 113
19
votes
3 answers

Is there any downside to marking all C++ constructors explicit?

A few times, when refactoring code, I have forgotten to add the explicit keyword when adding a parameter to a previously-parameterless constructor, or removing parameters from a previously multi-parameter constructor. To prevent this, I have gotten…
Dave Mateer
  • 17,608
  • 15
  • 96
  • 149
19
votes
1 answer

Explicit default constructors in C++17

In C++17, empty tag types in the standard library now have default constructors which are marked explicit, and are also = default. For example, std::piecewise_construct_t is now defined as struct piecewise_construct_t { explicit…
Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
19
votes
5 answers

Prevent undesired conversion in constructor

According to here, explicit: Specifies constructors and conversion operators (since C++11) that don't allow implicit conversions or copy-initialization. Thus, are these two techniques identical? struct Z { // ... Z(long long); …
johnbakers
  • 24,158
  • 24
  • 130
  • 258
19
votes
3 answers

C++11: in-class initializaton with "= {}" doesn't work with explicit constructor

In C++11 we can do in-class initialization using a "brace-or-equal-initializer" (words from the standard) like this: struct Foo { /*explicit*/ Foo(int) {} }; struct Bar { Foo foo = { 42 }; }; But if we un-comment explicit, it no longer…
1
2 3 4 5