0

BLOT: C++ has implicit conversion, I am looking for a way to prevent it.

Let me take an example for the same, for the code snippet below:

#include <iostream>

int incrementByOne(int i) {
    return ++i;
}

int main()
{
    bool b = true;
    
    std::cout << incrementByOne(b) << std::endl;

    return 0;
}

It would output: 2

How can I prevent such implicit conversions and be strict about taking only int as an argument even at runtime?

One way I can think of is overload the function. So the new code would look like:

#include <iostream>

int incrementByOne(int i) {
    return ++i;
}

int incrementByOne(bool) {
    std::cerr << "Please provide integer as argument" << std::endl;
    exit(0);
}

int incrementByOne(char) {
    std::cerr << "Please provide integer as argument" << std::endl;
    exit(0);
}

int main()
{
    bool b = true;
    
    std::cout << incrementByOne(b) << std::endl;

    return 0;
}

Is there any other(recommended) way of preventing implicit conversions at runtime?

  • 1
    Possible duplicate of https://stackoverflow.com/questions/12877546/how-do-i-avoid-implicit-conversions-on-non-constructing-functions – H Krishnan Jul 16 '20 at 05:35

2 Answers2

5

You can do this with the help of template<> and the delete operator:

#include <iostream>

int incrementByOne(int x) {
    return ++x;
}

template<class T>
T incrementByOne(T) = delete; // deleting the function

int main(void) {
    std::cout << incrementByOne(-10) << std::endl;
    std::cout << incrementByOne('a') << std::endl; // will not compile

    return 0;
}

After this, the parameter to be passed to the function must be an integer.

Supposing a floating point value was given in the function argument, you'll get the error:

main.cpp: In function 'int main()':
main.cpp:11:36: error: use of deleted function 'T incrementByOne(T) [with T = double]'
   11 |     std::cout << incrementByOne(3.5) << std::endl;
      |                                    ^
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
2

How can I prevent such implicit conversions and be strict about taking only int as an argument even at runtime?

You can static_assert if the passed argument is not integer for a template function. Check the type using std::is_same.

#include <type_traits> // std::is_same

template<typename Type>
Type incrementByOne(Type x)
{
    static_assert(std::is_same<int, Type>::value, " Some valuable messages");
    return ++x;
}
LernerCpp
  • 401
  • 1
  • 5
  • 13