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

  • "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. – Mast May 28 at 16:16
  • Related: Not notified of unwanted edits to question – Mast May 28 at 16:18
  • @Mast, there are 6 questions with both tags, and all clearly want constant-expression, rather than constants. I've read the post, btw. – Incomputable May 28 at 16:18
  • I'm not sure about the new tag name, I think compile-time-expression might a good candidate too. – Incomputable May 28 at 16:24
  • 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? – hoffmale May 29 at 0:37
  • @hoffmale, thanks, you're right. Adjusted the wording. – Incomputable May 29 at 5:01
up vote 6 down vote accepted

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.

  • 1
    I believe we should wait for a week (?) for the community consensus. I like your answer though. – Incomputable May 29 at 11:01

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 .

  • 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. – Incomputable May 29 at 21:12
  • 2
    Actually, the constexpr tag already exists; the proposal is not about creating it, but about changing it so it doesnt alias to constants anymore. – hoffmale May 30 at 10:29
  • 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! – Incomputable Jun 5 at 19:35

You must log in to answer this question.

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