Categories
c++ c++-faq constructor explicit explicit-constructor

What does the explicit keyword mean?

3447

What does the explicit keyword mean in C++?

7

  • 200

    I just want to point out to anyone new coming along that ever since C++11, explicit can be applied to more than just constructors. It’s now valid when applied to conversion operators as well. Say you have a class BigInt with a conversion operator to int and an explicit conversion operator to std::string for whatever reason. You’ll be able to say int i = myBigInt;, but you’ll have to cast explicitly (using static_cast, preferably) in order to say std::string s = myBigInt;.

    – chris

    Aug 30, 2012 at 16:52

  • 2

    Can’t explicit also refer to assignment? (i.e. int x(5);)

    Feb 27, 2014 at 5:10

  • @chris There is an explicit keyword that can be used on the declaration of an implicit conversion.

    Jun 20, 2018 at 15:52

  • @curiousguy: What do you mean? All conversions should be implicit? To let loose all sorts of silently applied funny miscasts due to accidental ambiguities? (See e.g. the “The safe bool problem” section at this C++ref. page, or open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2333.html for (much) more details on why “Explicit conversion is an ill defined concept.” is an ill-conceived statement.)

    – Sz.

    Aug 2, 2019 at 21:53


  • 1

    @Milan, Yes, that’s exactly it. If you’re looking for more information, this answer writes it up more formally. Do note that bool is special in this regard. Those answers and searching “explicit conversion operators” will lead you to more writeups about this feature and be better suited than a comment chain.

    – chris

    Jan 26, 2021 at 1:59

3929

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
{
private:
  int m_foo;

public:
  // single parameter constructor, can be used as an implicit conversion
  Foo (int foo) : m_foo (foo) {}

  int GetFoo () { return m_foo; }
};

Here’s a simple function that takes a Foo object:

void DoBar (Foo foo)
{
  int i = foo.GetFoo ();
}

and here’s where the DoBar function is called:

int main ()
{
  DoBar (42);
}

The argument is not a Foo object, but an int. However, there exists a constructor for Foo that takes an int so this constructor can be used to convert the parameter to the correct type.

The compiler is allowed to do this once for each parameter.

Prefixing the explicit keyword to the constructor prevents the compiler from using that constructor for implicit conversions. Adding it to the above class will create a compiler error at the function call DoBar (42). It is now necessary to call for conversion explicitly with DoBar (Foo (42))

The reason you might want to do this is to avoid accidental construction that can hide bugs.
Contrived example:

  • You have a MyString class with a constructor that constructs a string of the given size. You have a function print(const MyString&) (as well as an overload print (char *string)), and you call print(3) (when you actually intended to call print("3")). You expect it to print “3”, but it prints an empty string of length 3 instead.

7

  • 232

    nice write up, you might want to mention multi-arg ctors with default params can also act as single arg ctor, e.g., Object( const char* name=NULL, int otype=0).

    – maccullt

    Sep 24, 2008 at 1:23

  • 550

    I think it should also be mentioned that one should consider making single argument constructors explicit initially (more or less automatically), and removing the explicit keyword only when the implicit conversion is wanted by design. I think contructors should be explicit by default with an ‘implicit’ keyword to enable them to work as implicit conversions. But that’s not how it is.

    Aug 26, 2009 at 17:47

  • 12

    @thecoshman: You don’t declare a parameter explicit — you declare a constructor explicit. But yes: your parameters of type Foo have to be constructed explicitely, they won’t be silently constructed by just plugging their constructor’s parameters into the function.

    Jun 17, 2011 at 12:12


  • 94

    Just an FYI that when calling “print(3)” in your example, the function needs to be “print(const MyString &”). The “const” is mandatory here because 3 is converted to a temporary “MyString” object and you can’t bind a temporary to a reference unless it’s “const” (yet another in a long list of C++ gotchas)

    – Larry

    Jul 10, 2012 at 12:52

  • 54

    For completeness sake, I am adding that in addition to parameter conversion the explicit keyword here will also prevent the use of assignment form of a copy ctor (e.g., Foo myFoo = 42;) and require the explicit forms Foo myFoo = Foo(42); or Foo myFoo(42);

    – Arbalest

    Dec 15, 2012 at 0:53

1278

Suppose, you have a class String:

class String {
public:
    String(int n); // allocate n bytes to the String object
    String(const char *p); // initializes object with char *p
};

Now, if you try:

String mystring = 'x';

The character 'x' will be implicitly converted to int and then the String(int) constructor will be called. But, this is not what the user might have intended. So, to prevent such conditions, we shall define the constructor as explicit:

class String {
public:
    explicit String (int n); //allocate n bytes
    String(const char *p); // initialize sobject with string p
};

5

  • 18

    And it’s worth noting that the new generalized initialization rules of C++0x will make String s = {0}; ill-formed, rather than trying to call the other constructor with a null pointer, as String s = 0; would do.

    Dec 13, 2010 at 8:59


  • 10

    Even though this is an old question it seems worth pointing a few things out (or having someone set me straight). By making the int form, or both ctors, ‘explicit’ you would still have the same bug if you used String mystring('x') when you meant String mystring("x") wouldn’t you? Also, from the comment above I see the improved behavior of String s = {0} over String s = 0 thanks to making the int form of the ctor ‘explicit’. But, other than knowing the precedence of the ctors how do you know the intent (i.e. how to spot the bug) of this String s{0} ?

    – Arbalest

    Aug 5, 2013 at 5:23

  • Why String mystring = ‘x’; is getting converted to int?

    Mar 22, 2015 at 19:51


  • 12

    @InQusitive: 'x'is being treated as an integer because the char data type is just a 1-byte integer.

    – DavidRR

    Apr 30, 2015 at 13:55


  • 11

    The problem with your example is that it only works with copy initialization (using =) but not with direct initialization (without using =): the compiler will still call the String(int) constructor without generating an error if you write String mystring('x');, as @Arbalest pointed out. The explicit keyword is meant for preventing implicit conversions that happen in direct initialization and function resolution. A better solution to your example would be a simple overload of the constructor: String(char c);.

    – Maggyero

    Aug 11, 2015 at 20:24


184

In C++, a constructor with only one required parameter is considered an implicit conversion function. It converts the parameter type to the class type. Whether this is a good thing or not depends on the semantics of the constructor.

For example, if you have a string class with constructor String(const char* s), that’s probably exactly what you want. You can pass a const char* to a function expecting a String, and the compiler will automatically construct a temporary String object for you.

On the other hand, if you have a buffer class whose constructor Buffer(int size) takes the size of the buffer in bytes, you probably don’t want the compiler to quietly turn ints into Buffers. To prevent that, you declare the constructor with the explicit keyword:

class Buffer { explicit Buffer(int size); ... }

That way,

void useBuffer(Buffer& buf);
useBuffer(4);

becomes a compile-time error. If you want to pass a temporary Buffer object, you have to do so explicitly:

useBuffer(Buffer(4));

In summary, if your single-parameter constructor converts the parameter into an object of your class, you probably don’t want to use the explicit keyword. But if you have a constructor that simply happens to take a single parameter, you should declare it as explicit to prevent the compiler from surprising you with unexpected conversions.

1

  • 9

    useBuffer expects an lvalue for his argument, useBuffer(Buffer(4)) will not work either because of it. Changing it to take a const Buffer& or Buffer&& or just Buffer would make it work.

    – pqnet

    Jun 26, 2017 at 13:18