dhaumann dhaumann - 5 months ago 33
C++ Question

Is removing 'explicit' from a constuctor binary compatible?

An external library we are using contains the following explicit constructor:

class Chart {
explicit Chart(Chart::Type type, Object *parent);
// ...

The compiler complains with the following warning:

chart.h: warning #2305: declaration of 'explicit' constructor
without a single argument is redundant

Is it binary compatible to just remove the
keyword in chart.h without recompiling the library to avoid the warning? My feeling is that it's safe, since
does not make sense in this case anyways. Can anyone confirm?


Your best bet by a country mile is to switch off that warning for the duration of that inclusion if you get my meaning. Don't hack the vendor code.

Using explicit for multi-argument constructors makes perfect sense in C++11 onwards since it can be used to stop implicit brace initialisation. Futhermore the standard doesn't say that removing explicit must preserve the layout of the class, so you must assume that removing explicit could break binary compatibility. Also, dropping it could change the behaviour of contrived SFINAE patterns as that constructor could become re-available in certain circumstances. See http://en.cppreference.com/w/cpp/language/sfinae.