Talk:cpp/language/constant expression
I think it would make sense to explicitly claim, either here on or http://en.cppreference.com/w/cpp/language/array , that this code:
const size_t tabsize = 50; int tab[tabsize];
is correct and tabsize does not necessarily have to be declared as constexpr.
This is not an obvious fact and I've already heard people claiming otherwise.
Well to be honest, even now I'm not 100% sure that this is correct... I'm fairly certain though.
Due to this uncertainty, and because of me not being sure in what exactly cases can a const-qualified non-constexpr variable be used in an array declaration to denote its size (certainly not always), I won't go forth and do the edits myself.
- this page is already half way there "only integral constant expressions can be used as array bounds", I think it just needs a micro-example under "integral constant expressions" showing this tabsize as one. --Cubbi (talk) 16:45, 9 January 2016 (PST)
- It's bullet 10.a in the chapter Converted constant expression that describes when a non-constexpr variable can be used in a constant expression. This is the most complicated bullet. I think it is more in need of an example. --D41D8CD98F (talk) 02:32, 10 January 2016 (PST)
Contents |
[edit] 6) The this pointer
After retroactively applying the resolution for CWG 1369 to C++11 I believe the C++11 rule regarding the this pointer in constant expressions would be effectively the same as in C++14. --Oktal (talk) 07:27, 3 May 2017 (PDT)
[edit] Evolution of subkinds of constant expression
C++14 dropped the terms literal constant expression, reference constant expression, and address constant expression, added the new term permitted result of a constant expression, and adjusted how constant expression is defined in terms of those. Oktal (talk) 11:51, 15 May 2017 (PDT)
[edit] Invalid evaluated expressions instead of "contained subexpressions"
The definition given here to core constant expression is:
A core constant expression is any expression that does not have any one of the following in any subexpression (ignoring unevaluated expressions such as the operand of sizeof or the right operand of builtin && when the left operand evaluates to false)
However, that definition is a bit confusing because, in the following snippet:
constexpr int fun() { throw f(); return 0; } constexpr int i = fun();
throw f()
is not a subexpression of fun()
. You have to read further to find:
A function call to a constexpr
function with arguments that do not produce a constant expression when substituted
Which is a rule that doesn't appear in the standard, but it is neccesarily to correctly define it.
Wouldn't be easier to just say, for example:
A core constant expression is any expression whose evaluation path does not evaluate any one of the following expressions (e.g., ignoring unevaluated expressions such as the operand ofsizeof
or the right operand of builtin {tt|&&}} when the left operand evaluates tofalse
)
and remove the additional rule?
Besides, that way of defining constant expressions helps to understand faster the "on-demand" nature of constant expressions and the fact that constexpr
functions are allowed to contain expressions not allowed at compilation-time if they are not used in compilation-time contexts, etc. — Preceding unsigned comment added by Peregringlk (talk • contribs)
- This is another C++11 remnant that we haven't yet gotten rid of. C++11 has the concept of function invocation substitution, which is no longer a thing in C++14. T. Canens (talk) 12:44, 14 September 2017 (PDT)
[edit] Core constant expression exists since C++11
Previous versions of the standard (C++98 and C++03) have not defined the term “core constant expression”. The whole section should be marked as (since C++11). Moreover, it contains constexpr many times which was also not defined by those versions. --Melebius (talk) 01:22, 21 September 2018 (PDT)