Explicit Specifier

Prevent C++ implicit conversion.

Introduction

In C++, the explicit keyword is used with constructors to prevent them from performing implicit conversions. Implicit means automatic. It happens without us explicitly specifying the compiler what to do. By adding the explicit keyword before a constructor, we force the compiler not to perform any implict conversions.

Exanple

The following declarations are legal. The compiler converts the single argument to the class beging constructed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Base {
public:
Base() {}
Base(int x) {}
Base(const char* s) {}
};


int main() {
Base b1 = 10; // equivalent to Base b1 = Base(10);
Base b2 = "C++"; // equivalent to Base b2 = Base("C++");

return 0;
}

To prevent such implicit conversions, we can declare constructors with explicit keyword. Thus, the previous declaration would be illegal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Base {
public:
Base() {}
explicit Base(int x) {}
explicit Base(const char* s) {}
};


int main() {
Base b1 = Base(10);
Base b2 = Base("C++");

return 0;
}

References

Author

Joe Chu

Posted on

2023-12-07

Updated on

2024-03-24

Licensed under

Comments