Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid implicit conversions on non-constructing functions?

How do I avoid implicit casting on non-constructing functions?
I have a function that takes an integer as a parameter,
but that function will also take characters, bools, and longs.
I believe it does this by implicitly casting them.
How can I avoid this so that the function only accepts parameters of a matching type, and will refuse to compile otherwise?
There is a keyword "explicit" but it does not work on non-constructing functions. :\
what do I do?

The following program compiles, although I'd like it not to:

#include <cstdlib>  //the function signature requires an int void function(int i);  int main(){      int i{5};     function(i); //<- this is acceptable      char c{'a'};     function(c); //<- I would NOT like this to compile      return EXIT_SUCCESS; }  void function(int i){return;} 

*please be sure to point out any misuse of terminology and assumptions

like image 858
Trevor Hickey Avatar asked Oct 13 '12 22:10

Trevor Hickey


People also ask

How do you stop implicit conversions in C++?

One of the most interesting features of C++ is to implicitly convert the argument of a called function to its formal parameter type. As an example consider: void Foo(double param);

What is an implicit conversion?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.

How do you use implicit conversions?

An implicit conversion from type S to type T is defined by an implicit value which has function type S => T , or by an implicit method convertible to a value of that type. Implicit conversions are applied in two situations: If an expression e is of type S , and S does not conform to the expression's expected type T .

Which of the following types of conversions is implicit in C++?

Type Conversion in C++ Implicit Type Conversion Also known as 'automatic type conversion'. Done by the compiler on its own, without any external trigger from the user.


2 Answers

Define function template which matches all other types:

void function(int); // this will be selected for int only  template <class T> void function(T) = delete; // C++11  

This is because non-template functions with direct matching are always considered first. Then the function template with direct match are considered - so never function<int> will be used. But for anything else, like char, function<char> will be used - and this gives your compilation errrors:

void function(int) {}  template <class T> void function(T) = delete; // C++11    int main() {    function(1);    function(char(1)); // line 12 }  

ERRORS:

prog.cpp: In function 'int main()': prog.cpp:4:6: error: deleted function 'void function(T) [with T = char]' prog.cpp:12:20: error: used here 

This is C++03 way:

// because this ugly code will give you compilation error for all other types class DeleteOverload { private:     DeleteOverload(void*); };   template <class T> void function(T a, DeleteOverload = 0);  void function(int a) {} 
like image 130
PiotrNycz Avatar answered Sep 23 '22 06:09

PiotrNycz


You can't directly, because a char automatically gets promoted to int.

You can resort to a trick though: create a function that takes a char as parameter and don't implement it. It will compile, but you'll get a linker error:

void function(int i)  { } void function(char i); //or, in C++11 void function(char i) = delete; 

Calling the function with a char parameter will break the build.

See http://ideone.com/2SRdM

Terminology: non-construcing functions? Do you mean a function that is not a constructor?

like image 35
Luchian Grigore Avatar answered Sep 24 '22 06:09

Luchian Grigore