在标题view.h中:
template<class S, template<typename> class V>
void Operate(S c, const V<S>& vx);
template<class T, template<typename> class U>
class ViewBase
{
template<class S, template<typename> class V>
friend void Operate(S c, const V<S>& vx);
};
template<class T>
class ViewTypeA : public ViewBase<T, ViewTypeA>
{
};
template<class T>
class ViewTypeB : public ViewBase<T, ViewTypeB>
{
};
template<class S, template<typename> class V>
void Operate(S c, const V<S>& vx)
{
}
在cpp:
#include "view.h"
int main(int argc, char **argv)
{
Operate(5, ViewTypeA<int>());
Operate(5, ViewTypeB<int>());
}
gcc(使用-std = gnu ++ 11编译)会出错:
Build: Debug in codeblocks (compiler: GNU GCC Compiler) main.cpp||In function ‘int main(int, char**)’: main.cpp|7|error: call of overloaded ‘Operate(int, ViewTypeA&)’ is ambiguous main.cpp|7|note: candidates are: view.h|25|note: void Operate(S, const V&) [with S = int; V = ViewTypeA] view.h|9|note: void Operate(S, const V&) [with S = int; V = ViewTypeA; T = int; U = ViewTypeA]
main.cpp|8|error: call of overloaded ‘Operate(int, ViewTypeB)’ is ambiguous main.cpp|8|note: candidates are: view.h|25|note: void Operate(S, const V&) [with S = int; V = ViewTypeB] view.h|9|note: void Operate(S, const V&) [with S = int; V = ViewTypeB; T = int; U = ViewTypeA]
Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s))
类模板参数根本不应该出现 - 我是对的吗?模糊性似乎基于类的模板参数。