constexpr int bar (int x)
{
return x;
}
constexpr int foo (int x)
{
constexpr int j = constexpr (x);
return bar(j);
}
int x = constexpr foo(11);
Would that be allowed to work on constexpr function parameters?
constexpr int foo (int x)
{
constexpr int j = constexpr (x);
return bar(j);
}
prog.cc: In function 'int main()':
prog.cc:13:33: error: '<anonymous>' is not a constant expression
constexpr int &&val = get_val();
^
prog.cc:13:19: error: constexpr variable 'val' must be initialized by a constant expression
constexpr int &&val = get_val();
^ ~~~~~~~~~
prog.cc:13:19: note: reference to temporary is not a constant expression
prog.cc:13:25: note: temporary created here
constexpr int &&val = get_val();
^
Ah right, you also need to make it static. Sorry about that :)
I think of "if constexpr .. else .." as
if .. always holds then .. otherwise if it never holds.. otherwise error
Note that the "otherwise if" corresponds to "else". Not "else if" which would be "else if it never holds then if". I.e the "constexpr" determines a dependency mode for "if" which affects both branches.
Therefore the "constexpr" for else is not implied for me. Now if you put the "constexpr" into the condition, then that reads to me as
if .. neither always nor never holds then error otherwise whenever it holds .. otherwise whenever it doesn't hold...
Here, the "constexpr" just validates that we know of the constness similar to puttting the condition into "sizeof(char[1+cond])". But it doesn't affect the dependency mode: It's still checked with the dynamic control flow.
Am 31.03.2016 01:54 schrieb "Andrei L" <aend...@gmail.com>:
>
>
> 2016-03-31 1:32 GMT+05:00 Richard Smith <ric...@metafoo.co.uk>:
>>
>> The type of 'val' would be 'int &&'
>
>
> Oh. Is 'constexpr' applies 'const' to whole thing? i.e. 'int && const'?
>
I think it does that only for declarations of objects (i.e for object declarations with nonreference types).
Oh. Is 'constexpr' applies 'const' to whole thing? i.e. 'int && const'?Ah right, you also need to make it static. Sorry about that :)Heh :)After jumping through 'static' and '&&', we got what we wanted, but not quite. We turned simple local variable into static one.
On 4 April 2016 at 23:52, Paul Fultz II <pful...@gmail.com> wrote:
> Ultimately, I think the use of `constexpr if` is a poor term to use, causes
> confusion, and does not accurately describe what is required(that is the
> value must be known at compile-time *and* static). No doubt, `static_if`
> would better describe it and would be more consistent with the rest of the
> language(such as `static_assert`), however, there seems to be political
> issues with that name. Perhaps a better option could be to use something
> like angle brackets, since template parameters require compile-time values
> that are static:
For what it's worth, I decided against keeping calling it static_if (as I did
in http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4461.html), because
it's not the static if e.g. D has; a constexpr if has a much narrower
focus and can't
do all the bells and whistles D's static if can. My papers explain
why, but tl;dr is that
it turned out to be very hard to have such a control statement outside
a block scope,
and the issue of whether it should or should not establish a new scope
wasn't entirely
understood in Kona 2012, but became much better understood since.
I don't see the problem with "if constexpr".
Sure, you can think of it
as "the condition
must be compile-time and static",
I think of it as "the condition must
be a constant expression
convertible to bool".
A "static" doesn't seem to describe that any
better, quite the opposite.
However, the example Richard Smith gave required `static`.
Perhaps to make it clearer, `constexpr` variables should always imply `static`. <..> Would it ever make sense to have `constexpr` variables to be non-static?
This way `constexpr(...)` is like declaring an anonymous `constexpr` variable(which of course would mean its `static` as well).
2016-04-05 6:35 GMT+05:00 Paul Fultz II <pful...@gmail.com>:However, the example Richard Smith gave required `static`.The goal was to get non-const, non-static local variable initialized by result of constexpr function called at compile-time.
Perhaps to make it clearer, `constexpr` variables should always imply `static`. <..> Would it ever make sense to have `constexpr` variables to be non-static?I fail to see what compile-time constant has to do with linkage or storage duration. Are you mixing meaning of `constexpr` and `static` in the language and meaning of `static` outside the language?
This way `constexpr(...)` is like declaring an anonymous `constexpr` variable(which of course would mean its `static` as well).I don't think it should declare or act like it's declaring something. I think it should be just an assertion that something is going to be evaluated at compile-time, a strong guarantee.
A `constexpr` variable or expression can be used directly in an array or template parameter, so it must be static.