简体   繁体   中英

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 ).

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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