explicitIn C++, the explicit keyword is used to prevent implicit conversions or copy-initialization that the compiler might otherwise perform. It's primarily applied to constructors and conversion operators to make your code safer and more predictable.
When you declare a constructor as explicit, you are telling the compiler not to allow that constructor to be used for implicit conversions.
When you declare a constructor as explicit, you are telling the compiler not to allow that constructor to be used for implicit conversions.
explicit:#include <iostream>
class MyClass {
public:
MyClass(int x) {
std::cout << "Constructor called with " << x << std::endl;
}
};
void printMyClass(const MyClass& obj) {
std::cout << "In printMyClass function" << std::endl;
}
int main() {
MyClass obj = 42; // Implicit conversion from int to MyClass
printMyClass(100); // Implicit conversion and then passing to function
return 0;
}
MyClass(int x) is not marked as explicit.MyClass obj = 42;, where 42 is implicitly converted to a MyClass object.printMyClass(100); allows an implicit conversion from int to MyClass when calling the function.explicit:#include <iostream>
class MyClass {
public:
explicit MyClass(int x) {
std::cout << "Constructor called with " << x << std::endl;
}
};
void printMyClass(const MyClass& obj) {
std::cout << "In printMyClass function" << std::endl;
}
int main() {
// MyClass obj = 42; // Error: Implicit conversion is not allowed
MyClass obj(42); // OK: Direct initialization is allowed
// printMyClass(100); // Error: Implicit conversion is not allowed
printMyClass(MyClass(100)); // OK: Explicit conversion is required
return 0;
}
MyClass(int x) as explicit, you prevent the compiler from using it for implicit conversions.MyClass obj = 42; would result in a compile-time error because the conversion from int to MyClass requires an explicit call to the constructor.printMyClass(100); is also an error because the conversion from int to MyClass is not allowed implicitly; you need to explicitly construct the object by writing printMyClass(MyClass(100));.explicit?explicit, you force the programmer to make the conversion intentional, reducing the likelihood of errors.explicit, it makes the code's intention clearer to readers. It shows that certain operations require explicit acknowledgment from the programmer.explicit can also be used with conversion operators to prevent implicit type conversions.
#include <iostream>
class MyClass {
public:
explicit operator bool() const {
return true;
}
};
int main() {
MyClass obj;
// if (obj) { // Error: Implicit conversion to bool is not allowed
if (static_cast<bool>(obj)) { // OK: Explicit conversion is required
std::cout << "Object is true" << std::endl;
}
return 0;
}
bool() is marked as explicit, so if (obj) is not allowed.static_cast<bool>(obj) to perform the conversion.explicit MyClass(int x);explicit cast.explicit helps in preventing unintended conversions, improving code safety and clarity.Using explicit is a good practice in cases where you want to avoid automatic type conversions that could lead to unexpected results or bugs in your code.