c++ - Trouble understanding C++14 Relaxed constexpr restrictions -


i've came across new c++14 signature std::max function:

template< class t >  const t& max( const t& a, const t& b ); // (c++11)  template< class t >  constexpr const t& max( const t& a, const t& b );// (c++14) 

i've read relaxed constexpr restrictions proposal c++14 i'm still don't understand why function return value can constexpr

example:

std::vector<int> a, b; //this not compile understadnding of `constexpr` should int array[std::max(a.size(), b.size()]; (1) //this trivial use compile int array[std::max(1,2)]; (2) 

when calling std::max in (1) constexpr ignored ?

the base issue isn't directly related relaxed constexpr rules, constexpr function constant expression if arguments constant expressions. in second case:

int array[std::max(1,2)]; 

this works because integer literals indeed constant expressions.

c++11 more specific in case, draft c++11 standard in section 5.19 [expr.const] lays out cases sub-expression not considered constant expression , contains following:

an invocation of constexpr function arguments that, when substituted function invocation substitution (7.1.5), not produce constant expression;

in c++14 paragraph removed , have following exception:

an invocation of function other constexpr constructor literal class, constexpr function, or implicit invocation of trivial destructor (12.4) [ note: overload resolution (13.3) applied usual —end note ];

and have use rest of rules respect arguments(sub-expressions).

in first case:

int array[std::max(a.size(), b.size()]; 

std::vector::size not marked constexpr , falls under above quoted exception.

also note in section 7.1.5 [dcl.constexpr] have following:

a call constexpr function produces same result call equivalent non-constexpr function in respects except call constexpr function can appear in constant expression.

passing arguments constexpr function not constant expressions means expression not available uses in contexts require constant expression.

as dyp points out std::max not made constexpr in c++11 due to various issues including committee being conservative , limited time, can see of issues involved in n3039. being forgotten see n3856 says:

this short paper proposes make standard functions min , max constexpr. top on list of motivating cases al- lowing reference parameters constexpr functions in c++11. forgotten after core language change accepted

for reference know 1 , 2 constant expression section 5.19 says:

a literal constant expression prvalue core constant expression of literal type, not pointer type. integral constant expression literal constant expression of integral or unscoped enumeration type. [...]


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -