简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM