我想知道为什么以下功能模板专业不编译(由于no return statement in function returning non-void
)。
class Boring {
public:
template<typename T> bool eval() const { }
};
template<> inline bool Boring::eval<int>() const { return true; }
我希望除非使用,否则不会评估非特殊功能模板。如果将返回类型更改为T*
,则以下汇编成功。
int x = 5;
class Boring {
public:
template<typename T> T* eval() const { }
};
template<> inline int* Boring::eval<int>() const { return &x; }
两个程序均已良好。尽管如果主体eval
模板是实例化和调用,则会发生不确定的行为,但不会使程序不正确。但是,您的编译器以某种方式将其视为错误(也许是-Wall
),或者将此特定警告视为错误。默认情况下。