Search code examples
c++gccc++11overloadingdefault-parameters

C++ chooses 'wrong' overloaded method with default parameters


I have the following interface

virtual void send_command( const std::string& command, const std::string& key, const std::string& value, bool pipeline = true );    
virtual void send_command( const std::string& command, const std::string& key, bool pipeline = true );
virtual void send_command( const std::string& command, bool pipeline = true );

that is fully implemented in one class which I will then call as the following:

c.send_command("MUTLI");
c.send_command("SET", "foo", "bar");
c.send_command("GET", "foo");
c.send_command("EXEC");

When I check which method implementation get's called, I see that the third call (GET foo) ends up to hit the last implementation:

virtual void send_command( const std::string& command, bool pipeline = true );

where "foo" is implicit converted to bool. The same counts for "SET", "foo", "bar" which hits the second implementation (string, string, bool) When I make the call with an explicit c-cast to a string like this

c.send_command("GET", (std::string)"foo");

The expected method gets called.

I am using gcc (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8) with C++11.


Solution

  • You're hitting the fact that pointers can implicitly be converted to bool (testing that they're not null), and that a string literal is not a std::string, but a const char[] which decays into a const char* on call. What you can do is provide an additional overload taking a const char*:

    void send_command(const std::string& command, const char* key)
    { send_command(command, std::string(key)); }
    

    Live example