1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
In addition to the common tt(if (cond)) selection statement the tt(if
constexpr (cond)) syntax is supported by the language. Although it can be used
in all situations where a standard tt(if) selection statement are used, its
specific use is encountered inside function templates: ti(if constexpr) allows
the compiler to (conditionally) instantiate elements of a template function,
depending on the compile-time evaluation of the tt(if constexpr's (cond))
clause.
Here is an example:
verbinsert(-ns4 //ifconst examples/lambdaconstexpr.cc)
itemization(
it() At lines 7 and 9 tt(if constexpr) statements start. Since tt(value)
is a template non-type parameter its value is compile-time available,
and so are the values of the condition sections.
it() In line 15 tt(fun<4>()) is called: the condition in line 7 is
therefore tt(true), and the condition in line 9 is tt(false).
it() The compiler therefore instantiates tt(fun<4>()) this way:
verb( void fun<4>()
{
positive();
})
)
Note that the tt(if constexpr) statements themselves do not result in
executable code: it is used by the compiler to em(select) which part (or
parts) it should instantiate. In this case only tt(positive), which must be
available before the program's linking phase can properly complete.
|