Explicit Keyword

How does the explicit keyword help prevent unintended conversions?

The explicit keyword in C++ is used to prevent unintended implicit conversions. It can be applied to constructors and typecast operators to ensure that they are not called implicitly.

This enhances the safety and clarity of your code by making conversions explicit. Let's look at an example using a constructor:

#include <iostream>

class Vector {
public:
  float x, y, z;

  // Explicit constructor
  explicit Vector(float value)
    : x(value), y(value), z(value) {}
};

void Move(Vector direction) {
  std::cout << "Moving in direction: "
    << direction.x << ", "
    << direction.y << ", "
    << direction.z;
}

int main() {
  // This is allowed as we're explicitly
  // constructing a Vector
  Vector v1(1.0f);  

  // This is also explicit, so is allowed
  Move(Vector(1.0f));  

  // This woul require an implicit conversion
  // so it is not allowed
  Move(1.0f); 
}
error: 'void Move(Vector)': cannot convert argument 1 from 'float' to 'Vector'
note: Constructor for class 'Vector' is declared 'explicit'

In this example, the Vector constructor is marked as explicit, meaning you cannot pass a float directly to functions expecting a Vector without an explicit cast.

This prevents mistakes where a float might be mistakenly passed to a function expecting a Vector. Similarly, you can use explicit with typecast operators:

class MyType {
 public:
  // Explicit typecast operator
  explicit operator int() const { return 42; }
};

int main() {
  MyType obj;

  // Explicit conversion allowed
  int A = static_cast<int>(obj); 

  // Error: implicit conversion not allowed
  int B = obj;  
}
error: 'initializing': cannot convert from 'MyType' to 'int'
note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Here, the explicit typecast operator prevents implicit conversion to int. You must use static_cast<int>(obj) to perform the conversion explicitly.

By using explicit, you can make your code more robust and easier to understand, as it avoids unintended conversions that could lead to subtle bugs.

User Defined Conversions

Learn how to add conversion functions to our classes, so our custom objects can be converted to other types.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Overloading Typecast Operators
How do you overload a typecast operator in C++?
Example of Overloading Typecast Operator
Can you provide an example of overloading a typecast operator for a custom class?
Deleting Typecast Operators
Why might we want to delete a specific typecast operator?
Bool Typecasts
What special considerations are there for bool typecasts in C++?
Preventing Bool to Custom Type Conversion
How can we prevent a boolean from being converted to a custom type?
Forward Declaration with Conversions
How does forward declaration of a class work in the context of conversions?
Implementing Custom Type Conversion
How do you implement a custom type conversion that converts an object to a built-in type?
Preventing Constructor Calls
How can we use delete to prevent specific constructor calls?
Avoiding Implicit Conversion Bugs
Can you give an example where an implicit conversion might lead to a bug, and how to prevent it?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant