7
\$\begingroup\$

I'm proposing creating a new tag, constant-expression, and then making constexpr a synonym for constant-expression, rather than constants. Constant expression in C++ requires different usage of language and standard library, thus I believe it deserves a separate tag.

Background

C++11 has added a new keyword, constexpr. Although it was added later, C++98 already had constant expressions, albeit of extremely limited form. The keyword itself is just a specifier without constant expression.

How is it different from constants or just no tag at all?

I'll take one extreme here (and the lower one, if I'll be able to find it). Compile-time sieve of Eratosthenes. There is no template-metaprogramming involved, but constant-expression would fit in nicely instead of it.

  • no tag

A typical review would propose std::vector and adjustable index. The accepted answer shows the distinction in usage very well: std::array against std::vector, and std::index_sequence instead of index. In fact, the question would get downvoted or receive no votes at all, because people dislike macros in general.

  • constants

I believe it would fit if primes would be copy-pasted. The primes are not present in the code, but the computation itself is constant-expression. Some constants are constant-expressions (like const int size = 10), but most of them are not. I believe the tag overall fits for computation of well known numbers Euler's number, pi, and others. Primes might fall into that category, but then both constants and constant-expression should be put.

Effects

Most of the non-closed questions clearly imply constant-expression, not constants. This question looks like more preprocessor metaprogramming, than constants.

New tag wiki

A constant expression is a specific form of an expression, which is currently only available in C++. Use the tag when constexpr or other form of compile-time expression is implied in the question. The expressions are computed at compile-time, thus they're usable in contexts including, but not limited to

  • switch statement

  • C style array size

  • Template argument (only built-in types as of C++17)

constexpr keyword is often associated with constant-expression. It can be used in two contexts: variables and functions. In case of variables, if the expression is not constant-expression, declaration of the variables will cause compilation error. In usage as a specifier in function declarations, it tells about a function which can potentially be computed at compile time if the arguments are themselves constant-expressions.

Most of the time it is used in tandem with template-metaprogramming. Other common usage is compile-time computations. For more info, please see cppreference page about constant-expressions and constexpr

edit

I created new tag, and populated the wiki. I wanted to create compile-time-function-evaluation, but since I didn't have any general info about it, I decided to postpone it. I also think that @Gareth's idea is good, so the new synonym should be c++-constexpr, which is an alias for

\$\endgroup\$
6
  • \$\begingroup\$ "I believe the only change needed is to replace every occurrence of both c++ and constants tag into c++ and constant-expression." Blanket replacement like that smells like a bad idea, since old C++ questions could get affected the wrong way. \$\endgroup\$
    – Mast Mod
    Commented May 28, 2018 at 16:16
  • \$\begingroup\$ Related: Not notified of unwanted edits to question \$\endgroup\$
    – Mast Mod
    Commented May 28, 2018 at 16:18
  • \$\begingroup\$ @Mast, there are 6 questions with both tags, and all clearly want constant-expression, rather than constants. I've read the post, btw. \$\endgroup\$ Commented May 28, 2018 at 16:18
  • \$\begingroup\$ I'm not sure about the new tag name, I think compile-time-expression might a good candidate too. \$\endgroup\$ Commented May 28, 2018 at 16:24
  • \$\begingroup\$ Re: "Constants are constant-expressions": Can you please elaborate what you refer to as "constants"? Especially in the context of C++, if I take constant to mean a const-specified object it just means that it has to be initialized before its first usage (which might actually happen during runtime just before the first usage, see dynamic initialization), which directly contradicts the statement as they cannot be constant-expressions (whose value would be fixed at compile time). Do you mean literals instead? \$\endgroup\$
    – hoffmale
    Commented May 29, 2018 at 0:37
  • \$\begingroup\$ @hoffmale, thanks, you're right. Adjusted the wording. \$\endgroup\$ Commented May 29, 2018 at 5:01

2 Answers 2

6
\$\begingroup\$

Introduce the tag constant-expression

As addressed in the proposal, C++ constexpr functions are a very different concept compared to constants, so this warrants a new tag to describe this style of programming, especially considering how different one has to implement those functions.

Suggestion

Having the compiler execute expressions at compile time is hardly something new (e.g. C++ template meta programming) nor unique to C++ (e.g. D's CTFE = compile time function evaluation).

To address this, we should have an overarching tag that any similar functionality (like TMP or CTFE) can be grouped under. This would also be a place for future additions (e.g. if we were to introduce a ctfe tag for D).

So, my suggestion: Add a new tag compile-time-evaluation, which is currently superordinate to constant-expression and template-meta-programming.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I believe we should wait for a week (?) for the community consensus. I like your answer though. \$\endgroup\$ Commented May 29, 2018 at 11:01
1
\$\begingroup\$

If the proposed tag is specific to the C++ constexpr keyword (as it seems to be) then its name should contain "C++" somewhere in order to make that clear, for example .

\$\endgroup\$
3
  • \$\begingroup\$ I made lite search, and haven’t any similarly named tag. Also, as mentioned in the proposal, the gist is constant-expression, not keyword constexpr. May be other languages will have it later. \$\endgroup\$ Commented May 29, 2018 at 21:12
  • 2
    \$\begingroup\$ Actually, the constexpr tag already exists; the proposal is not about creating it, but about changing it so it doesnt alias to constants anymore. \$\endgroup\$
    – hoffmale
    Commented May 30, 2018 at 10:29
  • \$\begingroup\$ I actually decided to incorporate both answers. After looking at it after a week, for some reason it got into a good idea. Sorry, I can't remove the upvote since the answer is not modified (but I guess it doesn't matter anyway). Thanks for the answer! \$\endgroup\$ Commented Jun 5, 2018 at 19:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .