相关疑难解决方法(0)

这种模板函数重载的情况使我无法理解

#include <iostream>

template<typename T>
struct identity
{
    typedef T type;
};

template<typename T> void bar(T) { std::cout << "a" << std::endl; }
template<typename T> void bar(typename identity<T>::type) { std::cout << "b" << std::endl; }

int main ()
{
    bar(5); // prints "a" because of template deduction rules
    bar<int>(5); // prints "b" because of ...?

    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

我预计bar<int>(5)至少会产生歧义.这里涉及到关于模板函数重载决策的疯狂规则?

c++ templates language-lawyer overload-resolution

30
推荐指数
1
解决办法
442
查看次数