c++ - Why do templates allow constexpr function members with non-constexpr constructors? -
using c++14. why compile:
template<unsigned n> constexpr bool foo() { std::array<char, n> arr; homecoming true; }
but not this?
constexpr bool foo() { std::array<char, 10> arr; // non-constexpr constructor 'array' cannot used in constant look homecoming true; }
§7.1.5 [dcl.constexpr]/p6:
if instantiated template specialization of constexpr
function template or fellow member function of class template fail satisfy requirements constexpr
function or constexpr
constructor, specialization still constexpr
function or constexpr
constructor, though phone call such function cannot appear in constant expression. if no specialization of template satisfy requirements constexpr
function or constexpr
constructor when considered non-template function or constructor, template ill-formed; no diagnostic required.
it valid constexpr
function templates have specializations not satisfy constexpr
requirements, , valid utilize specializations long not in context requires constant expression.
it isn't valid, however, if no specialization of template satisfy constexpr
requirements. since in general case impossible determine whether possible instantiations of function template fail satisfy constexpr
requirements,the standard doesn't require diagnostic. hence, code ill-formed no diagnostic required - compiler can, not required to, study error.
c++ templates c++14 constexpr
No comments:
Post a Comment