4

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

1
  • It should be 1 question at a time so can you pick just one Commented May 29, 2015 at 9:17

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.