为什么被认为是部分专业化
Why regarded as partial specialized
有了下面的代码,我有点困惑为什么第二个foo
被认为是部分专门化,而后者不是(IMO都不应该是部分)。
template <bool IS_TRUE>
int foo();
// Doesn't work!?
template <bool IS_TRUE>
int foo<IS_TRUE>() {
return 0;
}
template <>
int foo<true>() {
return 0;
}
int main() {
return foo<true>();
}
在第二个foo
上gcc抱怨:
错误:函数模板局部特化' foo '不是允许
谁能解释一下,我错过了什么细节
名称后带有模板参数的语法为部分专门化保留:
template<typename T> struct foo {};
template<typename T> struct foo<T *> {};
如果部分专门化不能专门化任何参数,你将得到如下错误:
template<typename T> struct foo {};
template<typename T> struct foo<T> {};
error: partial specialization 'foo<T>' does not specialize any template arguments
函数模板不允许部分特化(使用重载代替),所以编译器没有必要检查特化是否真的特化了任何模板参数;无论如何,这都是违法的。如果将函数的声明和定义分开,则应该省略声明和定义中的模板参数:
template<bool IS_TRUE> int foo();
template<bool IS_TRUE> int foo() { return 0; }
template<> int foo<true>() { return 0; }
相关文章:
- 为什么rand()的使用被认为是不好的
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- 一个C头文件可以被认为是一个接口吗
- 为什么const char*和const char[]作为函数参数被认为是等价的
- 为什么12.0==11.999999999999999999被认为是真的
- 这会被认为是糟糕的编程实践吗?
- 为什么这被认为是恒定的?
- 抽象类/接口中的空方法是否被认为是一种好的做法?
- 如果我具有调用其其他实例之一的超载函数,它是否被认为是递归功能
- std::unique_ptr可以被认为是一个monad吗?
- C++ 指向类的指针和/或引用是否被认为是"movable"?
- 使用自定义宏来简化 cin 和 cout 语句等内容被认为是更好还是更差
- 使用成员函数更改对象或返回并分配它是否被认为是更好的做法?
- 我很难修复我认为是双重免费的东西
- 使用 #define 被认为是"bad practice"吗?
- 是一个被认为是不同类型的班级内部的正向声明
- 这被认为是有效的C 11还是C 14?还是GCC/Clang弄错了
- 为什么这个嵌套的 lambda 不被认为是 constexpr?
- 限制C++中允许的模板参数是否被认为是不良样式?
- 为什么被认为是部分专业化