Be explicit.

What happens with this C++ code?

class Foo
{
public:
	Foo(bool myDefault = true): default(myDefault) {}
	bool default;
};

void FunctionFoo(const Foo& foo)
{
	if(foo.default)
		cout << "default";
	else
		cout << "not default";
}

void main(){
	Foo* pFoo = new Foo(false);
	FunctionFoo(pFoo);
}

Any guesses?

Well it prints “default”. You may have expected it to print “not default”. Why is that, you ask?

Well, what happens is that in main FunctionFoo() gets passed a default constructed version of Foo. Not the Foo instance pointed to by pFoo. Quite a subtle bug I think. Especially because the compiler says nothing. How do you fix this problem. Easy. Put the keyword “explicit” in front of the constructor.

This has to do with the fact that C++ will implicitly convert parameters to methods/functions whenever it feels it can. See Effective C++ Item #18.

In C++ it is possible to declare constructors for a class, taking a single parameter, and use those constructors for doing type conversion. For example:

class A {
public:
	A(int);
};

void f(A) {}

void g()
{
	A a1 = 37;
        A a2 = A(47);
        A a3(57);
        a1 = 67;
        f(77);
}

A declaration like:

A a1 = 37;

says to call the A(int) constructor to create an A object from the integer value. Such a constructor is called a “converting constructor”.

However, this type of implicit conversion can be confusing, and there is a way of disabling it, using the keyword “explicit” in the constructor declaration:

class A {
public:
	explicit A(int);
};

void f(A) {}

void g()
{
	A a1 = 37; 	// illegal
        A a2 = A(47); 	// OK
        A a3(57);	// OK
        a1 = 67;	// illegal
        f(77);		// illegal
}

Using the explicit keyword, a constructor is declared to be “nonconverting”, and explicit constructor syntax is required:

class A {
public:
	explicit A(int);
};

void f(A) {}

void g()
{
	A a1 = A(37); 	// OK
        A a2 = A(47); 	// OK
        A a3(57);	// OK
        a1 = A(67);	// OK
        f(A(77));	// OK
}

Note that an expression such as:

A(47)

is closely related to function-style casts supported by C++. For example:

double d = 12.34;
int i = int(d);

While I “knew” this stuff, I’ve been bitten by hidden bugs due to implicit converting constructors a couple of times recently so I thought I’d share it just in case others were not aware or had forgotten.

I highly recommend using the explicit keyword on all your single parameter constructors unless you explicitly want to use C++’s implicit conversion feature. Personally I think this is a broken language feature. 99% of the time you don’t want implicit conversion. When you do you should have to specify an “implicit” conversion constructor eg:

class A {
public:
	implicit A(int);
};

But I’m not on the C++ standards body so I don’t get to make those decisions.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s