User Defined Conversions

Preventing Bool to Custom Type Conversion

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

Abstract art representing computer programming

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.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved