相关疑难解决方法(0)

重载的非类型模板是不明确的,而非模板化的函数是可以的

如果我们有一个模板函数,它接受类型的非类型参数,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)

c++ templates non-type

12
推荐指数
2
解决办法
506
查看次数

标签 统计

c++ ×1

non-type ×1

templates ×1