不明确的函数调用:将模板参数传递给多个覆盖函数之一
Ambiguous Function Call: Passing a template parameter to one of many override functions
以下是我正在处理的代码的缩写版本。我收到以下错误:
Call to member function 'AddValue' is ambiguous
我正在使用模板函数,因为我需要在某些情况下以不同方式处理某些类型,因此模板可以帮助我避免重复代码。
struct Event
{
void AddValue(const std::uint32_t& inValue)
{
// Ex: make inValue a string and insert key value pair in a map
}
void AddValue(const float& inValue)
{
// Ex: set restrictions on the precision of the float and insert key value pair in a map
}
};
template <typename TValue>
void manipulateEvent(const char* inCategory, const TValue& inValue)
{
Event e;
// other operations on "e"
e.AddValue(inValue);
Send(e);
}
void Log(const char* inCategory, const float& inValue)
{
manipulateEvent(inCategory, inValue);
}
// I believe this might be the source of the issue. Passing NULL into the
// template parameter in cases where there is nothing to pass.
// Without this it compiles fine. However, I do need this case.
void Log(const char* inCategory)
{
manipulateEvent(inCategory, NULL);
}
void Log(const char* inCategory, const std::uint32_t& inValue)
{
manipulateEvent(inCategory, inValue);
}
main()
{
Log("category", 10.5);
Log("category");
Log("category", 10);
}
有什么我遗漏的东西可以让我继续这个思路吗?或者对于如何实现相同但不同的目标有什么建议吗?
为 null char* 重载
提供适当的Log
void Log(const char* inCategory, const char*)
{
manipulateEvent(inCategory, nullptr);
}
然后通过明确传递给 Log
的内容来解决歧义。
带有文字后缀 f
和 u
Log("category", 10.5f);
Log("category", nullptr);
Log("category", 10u);
或通过铸造
Log("category", static_cast<float>(10.5));
Log("category", nullptr);
Log("category", static_cast<std::uint32_t>(10));
并且总是更喜欢 nullptr
而不是 NULL。
以下是我正在处理的代码的缩写版本。我收到以下错误:
Call to member function 'AddValue' is ambiguous
我正在使用模板函数,因为我需要在某些情况下以不同方式处理某些类型,因此模板可以帮助我避免重复代码。
struct Event
{
void AddValue(const std::uint32_t& inValue)
{
// Ex: make inValue a string and insert key value pair in a map
}
void AddValue(const float& inValue)
{
// Ex: set restrictions on the precision of the float and insert key value pair in a map
}
};
template <typename TValue>
void manipulateEvent(const char* inCategory, const TValue& inValue)
{
Event e;
// other operations on "e"
e.AddValue(inValue);
Send(e);
}
void Log(const char* inCategory, const float& inValue)
{
manipulateEvent(inCategory, inValue);
}
// I believe this might be the source of the issue. Passing NULL into the
// template parameter in cases where there is nothing to pass.
// Without this it compiles fine. However, I do need this case.
void Log(const char* inCategory)
{
manipulateEvent(inCategory, NULL);
}
void Log(const char* inCategory, const std::uint32_t& inValue)
{
manipulateEvent(inCategory, inValue);
}
main()
{
Log("category", 10.5);
Log("category");
Log("category", 10);
}
有什么我遗漏的东西可以让我继续这个思路吗?或者对于如何实现相同但不同的目标有什么建议吗?
为 null char* 重载
提供适当的Log
void Log(const char* inCategory, const char*)
{
manipulateEvent(inCategory, nullptr);
}
然后通过明确传递给 Log
的内容来解决歧义。
带有文字后缀 f
和 u
Log("category", 10.5f);
Log("category", nullptr);
Log("category", 10u);
或通过铸造
Log("category", static_cast<float>(10.5));
Log("category", nullptr);
Log("category", static_cast<std::uint32_t>(10));
并且总是更喜欢 nullptr
而不是 NULL。