简体   繁体   English

模板规范得到“对重载函数的不明确调用”

[英]Template specification get 'Ambiguous call to overloaded function'

I'm working on a template function that could return a Collection or the first element in Collection , which stored some native pointers.我工作的一个模板函数时可能会返回一个Collection或第一个元素Collection ,其中存储一些本地指针。


template<typename T>
class Collection
{
public:
    std::vector<T*> m_Container;
};

template<typename T>
T* GetGlobal()
{
    return &(*g_GlobalStrings.begin());
}

template<typename T>
Collection<T>* GetGlobal()
{
    auto res = std::vector<T*>(g_GlobalStrings.size());
    for (auto&& item : g_GlobalStrings)
    {
        res.push_back(&item);
    }
    return Collection<T>{res};
}

But compiler(MSVC) says:但是编译器(MSVC)说:

error C2668: 'GetGlobal': ambiguous call to overloaded function.

I'm not sure how to implement this.我不确定如何实现这一点。 Any help will be really appreciated.任何帮助将不胜感激。

Return type doesn't make difference on function signature so the call is ambigious. 返回类型对函数签名没有影响,因此 调用是模棱两可的。

You should use something like std::enable_if<>.你应该使用 std::enable_if<> 之类的东西。

The only way to prevent ambiguity is that not enabling both functions for the same type.防止歧义的唯一方法是不要为同一类型启用这两个功能。 Another simpler way is to change the function names.另一种更简单的方法是更改​​函数名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM