简体   繁体   中英

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.


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:

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<>.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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