如果我们有一个模板函数,它接受类型的非类型参数,int或者short编译器抱怨以下调用的歧义:
// Definition
template <int I> void foo() { std::cout << "I: " << I << '\n'; }
template <short S> void foo() { std::cout << "S: " << S << '\n'; }
// Usage
foo<0>(); // Ambiguous, int or short?
Run Code Online (Sandbox Code Playgroud)
起初我对这种行为并不感到惊讶,文字0可能是一个int或一个short,但如果我们尝试这个:
// Definition
void foo(int i) { std::cout << "i: " << i << '\n'; }
void foo(short s) { std::cout << "s: " << s << '\n'; } …Run Code Online (Sandbox Code Playgroud)