Preventing Bool to Custom Type Conversion

How can we prevent a boolean from being converted to a custom type?

To prevent a boolean from being converted to a custom type in C++, you can use the delete keyword to explicitly delete the constructor or typecast operator that would enable such a conversion.

This ensures that any attempt to perform the conversion results in a compilation error. Here's an example using a Vector class:

#include <iostream>

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

  // Constructor from float is allowed
  explicit Vector(float value)
    : x(value), y(value), z(value) {}

  // Constructor from bool is deleted
  Vector(bool) = delete;  
};

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

int main() {
  Vector v1(1.0f);
  Move(v1);

  // Error: constructor is deleted
  Vector v2(true);

  // Error: constructor is deleted
  Move(true);
}
error: attempting to reference a deleted function
note: 'Vector::Vector(bool)': function was explicitly deleted

In this example, the Vector class has a constructor that accepts a float but explicitly deletes the constructor that accepts a bool. This prevents the creation of a Vector from a boolean value.

Another way to prevent unwanted conversions is by deleting the typecast operator that allows the conversion. For example:

#include <iostream>

class CustomType {
 public:
  // Allow conversion to int
  explicit operator int() const { return 42; }

  // Delete conversion to bool
  operator bool() const = delete;  
};

int main() {
  CustomType obj;

  // Explicit conversion to int
  int value = static_cast<int>(obj);
  std::cout << "Value: " << value << "\n";

  // Error: conversion to bool is deleted
  bool flag = static_cast<bool>(obj); 
}
error: attempting to reference a deleted function
note: 'CustomType::operator bool(void) const': function was explicitly deleted

In this example, the CustomType class allows conversion to int but deletes the conversion to bool. This ensures that a CustomType object cannot be used in a boolean context, preventing unintended or illogical conversions.

By deleting specific constructors or typecast operators, you can control how your custom types are used and ensure that only meaningful and intended conversions are allowed.

This enhances the robustness and clarity of your code, making it easier to maintain and less prone to errors.

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?
Explicit Keyword
How does the explicit keyword help prevent unintended conversions?
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++?
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