Error: "2 overloads have similar conversions"
Error: "2 overloads have similar conversions"
编辑: 当我写这个问题时,我注意到方法 std::string GetNodeValue(const std::string& nodePath, const char * defaultValue)
不是常量。正如 LogicStuff 在他的评论中也提到的那样,添加 const
限定条件解决了歧义。
我知道这个问题已经被问过并正确回答了 here 和其他几次。我理解潜在的问题,但我不太明白为什么会在这种特殊情况下发生,它唤醒了我好奇的自我。
我有以下 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;
}
// ...
};
模板方法还针对 int16_t
、uint16_t
等类型进行了一些特化,直至 uint64_t
。
使用起来很神奇:
string someValue = configurationReaderPtr->GetNodeValue("some_noe", "");
uint32_t otherValue = configurationReaderPtr->GetNodeValue("other_node", 11000);
bool yetAnother = configurationReaderPtr->GetNodeValue("other_node", true);
除了一种情况:
uint32_t otherValue = configurationReaderPtr->GetNodeValue("other_node", 0);
我不断收到的错误是:
“2 个重载具有相似的转换
可以是 'std::string ConfigurationReader::GetNodeValue(const std::string &,const char *)' 或 'uint32_t ConfigurationReader::GetNodeValue(const std::string &,uint32_t) const'"
我尝试转换 "default" 值:uint32_t(0)
、static_cast<uint32_t>(0)
、0U
,但没有成功。
我应该指出,我已经找到了 解决方法:
uint32_t otherValue = 0;
otherValue = configurationReaderPtr->GetNodeValue("other_node", otherValue);
但这并不能回答我的好奇心。我目前正在使用 Microsoft Visual Studio 2012 Express 和 boost 1.54 库。
有什么想法吗?
在您的特定示例中,将字符串版本的签名更改为
似乎是明智的
std::string GetNodeValue(const std::string& nodePath, const std::string defaultValue)
这将消除任何歧义。
这是因为 0 是空指针的字面量(在现代 C++ 中被 "nullptr" 代替)。
所以 0 可以是 int 或空指针,尤其是 char*
编辑以添加一些参考:
您可以在标准中找到它
4.10 Pointer conversions
A null pointer constant is an integer literal (2.13.2) with value zero or a prvalue of type std::nullptr_t
(最后一个是对nullptr
的引用)
对于
,两种重载都被认为是同样可行的
configurationReaderPtr->GetNodeValue("other_node", 0);
因为:
需要从 0
到 const char *
类型的隐式转换。
需要从 ConfigurationReader*
到 ConfigurationReader const*
的隐式转换(以调用 const
-限定的成员函数)
使两个重载(相等)const
合格后,代码编译(首选函数模板)。第一次重载也不首先修改任何成员。
编辑: 当我写这个问题时,我注意到方法 std::string GetNodeValue(const std::string& nodePath, const char * defaultValue)
不是常量。正如 LogicStuff 在他的评论中也提到的那样,添加 const
限定条件解决了歧义。
我知道这个问题已经被问过并正确回答了 here 和其他几次。我理解潜在的问题,但我不太明白为什么会在这种特殊情况下发生,它唤醒了我好奇的自我。
我有以下 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;
}
// ...
};
模板方法还针对 int16_t
、uint16_t
等类型进行了一些特化,直至 uint64_t
。
使用起来很神奇:
string someValue = configurationReaderPtr->GetNodeValue("some_noe", "");
uint32_t otherValue = configurationReaderPtr->GetNodeValue("other_node", 11000);
bool yetAnother = configurationReaderPtr->GetNodeValue("other_node", true);
除了一种情况:
uint32_t otherValue = configurationReaderPtr->GetNodeValue("other_node", 0);
我不断收到的错误是: “2 个重载具有相似的转换 可以是 'std::string ConfigurationReader::GetNodeValue(const std::string &,const char *)' 或 'uint32_t ConfigurationReader::GetNodeValue(const std::string &,uint32_t) const'"
我尝试转换 "default" 值:uint32_t(0)
、static_cast<uint32_t>(0)
、0U
,但没有成功。
我应该指出,我已经找到了 解决方法:
uint32_t otherValue = 0;
otherValue = configurationReaderPtr->GetNodeValue("other_node", otherValue);
但这并不能回答我的好奇心。我目前正在使用 Microsoft Visual Studio 2012 Express 和 boost 1.54 库。
有什么想法吗?
在您的特定示例中,将字符串版本的签名更改为
似乎是明智的std::string GetNodeValue(const std::string& nodePath, const std::string defaultValue)
这将消除任何歧义。
这是因为 0 是空指针的字面量(在现代 C++ 中被 "nullptr" 代替)。
所以 0 可以是 int 或空指针,尤其是 char*
编辑以添加一些参考: 您可以在标准中找到它
4.10 Pointer conversions A null pointer constant is an integer literal (2.13.2) with value zero or a prvalue of type std::nullptr_t
(最后一个是对nullptr
的引用)
对于
,两种重载都被认为是同样可行的configurationReaderPtr->GetNodeValue("other_node", 0);
因为:
需要从
0
到const char *
类型的隐式转换。需要从
ConfigurationReader*
到ConfigurationReader const*
的隐式转换(以调用const
-限定的成员函数)
使两个重载(相等)const
合格后,代码编译(首选函数模板)。第一次重载也不首先修改任何成员。