The explicit keyword in C++ is used to prevent the compiler from performing automatic type conversions through constructors or conversion operators. It ensures that object creation and type conversion happen only when explicitly requested by the programmer.
- The explicit keyword is primarily used with single-parameter constructors and conversion operators.
- It prevents unintended implicit conversions and improves code safety.
- It makes the code more readable and less error-prone.
Why Do We Need explicit?
When a constructor accepts a single argument, the compiler can automatically use it for type conversion. This behavior may sometimes lead to unexpected results.
Example: Without explicit
#include <iostream>
using namespace std;
class Number{
public:
Number(int x) {
cout << "Constructor Called: " << x << endl;
}
};
int main() {
Number n = 10;
return 0;
}
Output
Constructor Called: 10
Using explicit Keyword
The explicit keyword prevents automatic conversions and requires direct object creation.
Syntax:
explicit ConstructorName(parameters);
#include <iostream>
using namespace std;
class Number{
public:
explicit Number(int x) {
cout << "Constructor Called: " << x << endl;
}
};
int main() {
Number n(10);
return 0;
}
Output
Constructor Called: 10
Explanation: The object is created directly using the constructor because implicit conversion is not allowed.
Effect of explicit on Implicit Conversion
#include <iostream>
using namespace std;
class Number{
public:
explicit Number(int x) {
cout << "Constructor Called" << endl;
}
};
int main() {
Number n = 10;
return 0;
}
Output:
Compilation Error
Explanation: The statement Number n = 10; attempts an implicit conversion from int to Number. Since the constructor is marked as explicit, the compiler generates an error.
explicit with Multiple Constructors
#include <iostream>
using namespace std;
class Student{
public:
explicit Student(int id) {
cout << "Student ID: " << id << endl;
}
Student(string name) {
cout << "Student Name: " << name << endl;
}
};
int main() {
Student s1(101);
Student s2("Rahul");
}
Output
Student ID: 101 Student Name: Rahul
Explanation: The integer constructor requires explicit object creation, while the string constructor can still be used for implicit conversions.
explicit with Conversion Operators (C++11)
Starting from C++11, the explicit keyword can also be applied to conversion operators.
#include <iostream>
using namespace std;
class Test{
public:
explicit operator bool() const {
return true;
}
};
int main() {
Test t;
if (static_cast<bool>(t))
cout << "True";
}
Output
True
Explanation: The object is explicitly converted to bool using static_cast, preventing accidental conversions.
Advantages of explicit Keyword
- Prevents unintended implicit conversions.
- Improves program reliability and safety.
- Makes code behavior more predictable.
- Enhances readability and maintainability.
- Helps avoid hard-to-find bugs.
Limitations of explicit Keyword
- Object creation and type conversions must be performed explicitly, which can increase code verbosity.
- Additional type casting may be required when conversions are intended.
- In some cases, it can reduce convenience by preventing automatic conversions that are actually safe and desirable.