Programming with Passion

Make the best out of everything.

Tuesday, 22 March 2016

Keyword explicit

Keyword explicit

On a function call, C++ allows one implicit conversion to happen for each argument. This may be somewhat problematic for classes, because it is not always what is intended. For example, if we add the following function to the last example:

 
void fn (B arg) {}


This function takes an argument of type B, but it could as well be called with an object of type A as argument:

 
fn (foo);


This may or may not be what was intended. But, in any case, it can be prevented by marking the affected constructor with the explicit keyword:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// explicit:
#include <iostream>
using namespace std;

class A {};

class B {
public:
  explicit B (const A& x) {}
  B& operator= (const A& x) {return *this;}
  operator A() {return A();}
};

void fn (B x) {}

int main ()
{
  A foo;
  B bar (foo);
  bar = foo;
  foo = bar;
  
//  fn (foo);  // not allowed for explicit ctor.
  fn (bar);  

  return 0;
}


Additionally, constructors marked with explicit cannot be called with the assignment-like syntax; In the above example,bar could not have been constructed with:

 
B bar = foo;


Type-cast member functions (those described in the previous section) can also be specified as explicit. This prevents implicit conversions in the same way as explicit-specified constructors do for the destination type.

No comments:

Post a Comment