2

i have two functions for return value error handling:

static void handleError(int error, const std::string& message, bool bShowAll = false);

and

static void handleError(int error, const std::string& prefix, const std::string& message, bool bShowAll = false)

the call for this functions that i have the problem looks like this:

handleError(errro, "moduleA", "query failed");

now the problem pops up, that the invocation above leads into calling the first variant instead of the second one with a pass parameter of bShowAll resolved to "true". my best guess is that a "const char[]" is compatible with the "bool" type.

i have tried changing the order of the functions (by using a forward declaration for the second) but it did not help at all. what other options does c++ offer to solve that? (having type casts all around did not work - using some other type than bool, e.g. an enum-type with enum-symbol-equivalents for what bool is designed for went into nice operation. i think the default-value parameter init is the item that opened the door for this but MSVC 2012 itself did not hint for that ambiguity despite i am running it with warning level up to #4.)

note: i think stack-overflow is as well about learning how to do things nice, smart and also for learning from others: getting an own understanding on how things are designed in the area of computing and computer languages.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Alexander Stohr
  • 159
  • 1
  • 18
  • This is quite strange. May you try with passing the last argument as false itself. – Rizwan Sep 10 '18 at 09:46
  • 1
    You unfortunately can not disable implicit conversions for non constructors functions. What you can do is declare an overload for const char* and do the conversion there. – gan_ Sep 10 '18 at 09:47
  • 2
    Using an enum type is a good solution, in my opinion. `ShowAllErrors` is much more informative than `true`. (`bool` is the most overrated parameter type.) – molbdnilo Sep 10 '18 at 09:48
  • 1
    Possible duplicate of [Overloaded Bool/String Ambiguity](https://stackoverflow.com/questions/26413951/overloaded-bool-string-ambiguity) – Alan Birtles Sep 10 '18 at 09:56
  • i saw the "possible duplicate" item and i think its valid. but i feel like i want to abstain from klicking "that solve my problem" button because here in the topic we have a few more substantially well designe explanations, proposals and solutions going beyond what the other topic did provide. i dont want to make this content here "unavailable". – Alexander Stohr Sep 11 '18 at 08:39

1 Answers1

3

The problem is that "query failed" is of type const char[], it needs to be converted to std::string for handleError to be called. This is a user-defined conversion and has worse rank than the standard conversion (from const char[] to const char* and to bool).

You can make it passing a std::string explicitly, to avoid the implicit user-defined conversion.

handleError(errro, std::string("moduleA"), std::string("query failed"));

Or since C++14

using namespace std::string_literals;
handleError(errro, "moduleA"s, "query failed"s);
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • thanks for the explanation - it sheds much light on how this came into effect. so i feel like having a c++ standard specific macro that is like this might be an option for solving this the smartest/latest fashion possible: #define S(_name) _name s #define S(_name) std::string(_name) (okay, i hoped to find some other non-code-invasive solution - for now the above indicated non-bool solution seems to fit this my personal goal best. and it gets rid of "bool" where its never sure what it will mean.) – Alexander Stohr Sep 10 '18 at 15:08
  • the proposed page (Alan, now linked on top) gave me a nice _real_ solution for "const char []" - its called delegating: "If you have C++11 you can use a delegating constructor: A(char const* s) : A(std::string(s)) { }" – Alexander Stohr Sep 10 '18 at 15:15