mardi 2 février 2016

Error: "2 overloads have similar conversions"

EDIT: As I was writing the question I noticed that the method std::string GetNodeValue(const std::string& nodePath, const char * defaultValue) wasn't const. As LogicStuff also mentioned in his comment, adding the const qualification resolved the ambiguity.

I know this question was already been asked and answered properly here and several other times. I understand the underlying problem but I can't quite figure out why it is happening in this particular case and it has awoken my curious self.

I have the following class:

class ConfigurationReader
{
public:
    // ...   
    std::string GetNodeValue(const std::string& nodePath, const char * defaultValue)
    {
        const std::string temp(defaultValue);
        return GetNodeValue(nodePath, temp); 
    }    

    template <typename T> T GetNodeValue(const std::string & nodePath, T defaultValue) const 
    {
        boost::optional<T> nodeValue = configuration.getNodeValueNothrow<T>(nodePath);
        if ( nodeValue ) 
        {
            return *nodeValue;
        }
        LogConfigurationProblemsCri(logger, "Node not found: " << nodePath << ", Default value: " << defaultValue);
        return defaultValue;
    }
    // ...    
};

The template method has also several specializations for the types int16_t, uint16_t, and so on up to uint64_t.

It works like a charm when used:

string someValue = configurationReaderPtr->GetNodeValue("some_noe", "");
uint32_t otherValue = configurationReaderPtr->GetNodeValue("other_node", 11000);
bool yetAnother = configurationReaderPtr->GetNodeValue("other_node", true);

Except in one case:

uint32_t otherValue = configurationReaderPtr->GetNodeValue("other_node", 0);

The error I keep getting is: "2 overloads have similar conversions could be 'std::string ConfigurationReader::GetNodeValue(const std::string &,const char *)' or 'uint32_t ConfigurationReader::GetNodeValue(const std::string &,uint32_t) const'"

I tried casting the "default" value: uint32_t(0), static_cast<uint32_t>(0), 0U without any luck.

I should point out that I already found a workaround:

uint32_t otherValue = 0;
otherValue = configurationReaderPtr->GetNodeValue("other_node", otherValue);

But this doesn't answer my curiosity. I am currently using Microsoft Visual Studio 2012 Express and boost 1.54 libraries.

Any thoughts?

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire