Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++: implicit conversion order

I've a (member-)fuction overloaded like this:

bool foo(bool);
int foo(int);
float foo(float);
...
std::string foo( std::string const&);

for a couple of build-in-types but not for const char*. Calling foo("beauty is only skin-deep");, to my big suprise, called the bool-variant of the foo function. This leads to my questions:

QUESTION: Is there a well defined implicit conversion order for build-in-types

NOT THE QUESTION: How to avoid implicit conversion. How evil is implicit conversion. ...

EDIT: removed question about implicit converstion order for user-defined-questions

like image 713
user1235183 Avatar asked Mar 16 '23 21:03

user1235183


1 Answers

according to: http://en.cppreference.com/w/cpp/language/implicit_cast

all build-in conversions take place before user defined ones

pointer -> bool is a 'Boolean conversions' (needed for if(pointer) notation), last of 'numeric conversions'

'const char*' -> std::string is a 'user defined conversion' as from language point of view, std::string is a user defined type.

Unfortunately simplest solution is to write proper overload of fun(const char*), or to avoid fun(bool) vs fun(std::string) overloads

like image 197
Hcorg Avatar answered Mar 24 '23 17:03

Hcorg