简体   繁体   English

函数模板专门化中的布尔返回值

[英]bool return value in function template specialization

I am wondering why the below function template specialization does not compile (due to no return statement in function returning non-void ). 我想知道为什么下面的函数模板特化无法编译(由于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; }

I would expect that the non-specialized function template would not be evaluated unless used. 我希望除非使用,否则不会对非专业功能模板进行评估。 If the return type is changed to T* , the below compiles successfully. 如果返回类型更改为T* ,则以下代码将成功编译。

int x = 5;

class Boring {
public:
    template<typename T> T* eval() const { }
};

template<> inline int* Boring::eval<int>() const { return &x; }

Both programs are well-formed. 这两个程序的格式都正确。 Although undefined behaviour would occur if the primary eval template were instantiated and invoked, that does not render the program ill-formed. 尽管如果实例化和调用主eval模板会发生未定义的行为,但这不会使程序格式错误。 However, you have your compiler in some mode where it treats some warnings as errors (perhaps -Wall ), or it treats this particular warning as an error by default. 但是,您的编译器处于某种模式,在这种模式下,它将某些警告视为错误(也许是-Wall ),或者默认情况下会将特定警告视为错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM