390

It seems to me that having a "function that always returns 5" is breaking or diluting the meaning of "calling a function". There must be a reason, or a need for this capability or it wouldn't be in C++11. Why is it there?

// preprocessor.
#define MEANING_OF_LIFE 42

// constants:
const int MeaningOfLife = 42;

// constexpr-function:
constexpr int MeaningOfLife () { return 42; }

It seems to me that if I wrote a function that return a literal value, and I came up to a code-review, someone would tell me, I should then, declare a constant value instead of writing return 5.

8
  • 29
    Can you do define a recursive function that returns a constexpr ? If so, I can see an usage.
    – ereOn
    Jan 20, 2011 at 14:24
  • 29
    I believe that the question should state "why introduce a new keyword (!) if the compiler can deduce for itself whether a function can be evaluated in compile time or not". Having it "guaranteed by a keyword" sounds good, but I think I'd prefer to have it guaranteed whenever it's possible, without the need for a keyword.
    – Kos
    Jan 20, 2011 at 15:25
  • 7
    @Kos : Somebody who is MORE conversant with C++ internals would probably prefer your question, but my question comes from a perspective of a person who has written C code before, but is not familiar with C++ 2011 keywords at all, nor C++ compiler implementation details. Being able to reason about compiler optimization and constant-expression-deduction is a subject for a more advanced-user question than this one.
    – Warren P
    Oct 29, 2012 at 14:33
  • 14
    @Kos I was thinking along the same lines as you, and the answer I came up with was, without constexpr, how would you (easily) know that the compiler actually compile-time-evaluated the function for you? I suppose you could check the assembly output to see what it did, but it's easier to just tell the compiler that you require that optimization, and if for some reason it can't do that for you, it will give you a nice compile-error instead of silently failing to optimize where you expected it to optimize. Jan 2, 2013 at 5:34
  • 4
    @Kos: You could say the same thing about const. In fact, mandated intent is useful! Array dimensions are the canonical example. Jun 4, 2013 at 12:21

15 Answers 15

351

Suppose it does something a little more complicated.

constexpr int MeaningOfLife ( int a, int b ) { return a * b; }

const int meaningOfLife = MeaningOfLife( 6, 7 );

Now you have something that can be evaluated down to a constant while maintaining good readability and allowing slightly more complex processing than just setting a constant to a number.

It basically provides a good aid to maintainability as it becomes more obvious what you are doing. Take max( a, b ) for example:

template< typename Type > constexpr Type max( Type a, Type b ) { return a < b ? b : a; }

Its a pretty simple choice there but it does mean that if you call max with constant values it is explicitly calculated at compile time and not at runtime.

Another good example would be a DegreesToRadians function. Everyone finds degrees easier to read than radians. While you may know that 180 degrees is 3.14159265 (Pi) in radians it is much clearer written as follows:

const float oneeighty = DegreesToRadians( 180.0f );

Lots of good info here:

http://en.cppreference.com/w/cpp/language/constexpr

8
  • 21
    Excellent point with it telling the compiler to try and calculate the value at compile time. I'm curious why const doesn't provide this functionality when specific optimizations are specified? Or does it? Jan 20, 2011 at 14:37
  • 12
    @Tamus: Often it will but its not obliged to. constexpr obliges the compiler and will spit out an error if it can't.
    – Goz
    Jan 20, 2011 at 14:38
  • 22
    I see it now. Sin(0.5) is another. This replaces C macros neatly.
    – Warren P
    Jan 20, 2011 at 21:19
  • 13
    I can see this as a new interview question: Explain the differences between the const and constexpr keyword.
    – Warren P
    Jan 20, 2011 at 21:30
  • 3
    As a way of documenting this point for myself I wrote similar code as above and again with the function being "const" rather than "constexpr". As I am using Clang3.3, -pedantic-errors and -std=c++11 I expected the latter would not compile. It compiled and ran as in the "constexpr" case. Do you suppose this is a clang extension or has there been a tweak to the C++11 spec since this post was answered?
    – Arbalest
    Jul 24, 2013 at 22:44
199

Introduction

constexpr was not introduced as a way to tell the implementation that something can be evaluated in a context which requires a constant-expression; conforming implementations has been able to prove this prior to C++11.

Something an implementation cannot prove is the intent of a certain piece of code:

  • What is it that the developer want to express with this entity?
  • Should we blindly allow code to be used in a constant-expression, just because it happens to work?

What would the world be without constexpr?

Let's say you are developing a library and realize that you want to be able to calculate the sum of every integer in the interval (0,N].

int f (int n) {
  return n > 0 ? n + f (n-1) : n;
}

The lack of intent

A compiler can easily prove that the above function is callable in a constant-expression if the argument passed is known during translation; but you have not declared this as an intent - it just happened to be the case.

Now someone else comes along, reads your function, does the same analysis as the compiler; "Oh, this function is usable in a constant-expression!", and writes the following piece of code.

T arr[f(10)]; // freakin' magic

The optimization

You, as an "awesome" library developer, decide that f should cache the result when being invoked; who would want to calculate the same set of values over and over?

int func (int n) { 
  static std::map<int, int> _cached;

  if (_cached.find (n) == _cached.end ()) 
    _cached[n] = n > 0 ? n + func (n-1) : n;

  return _cached[n];
}

The result

By introducing your silly optimization, you just broke every usage of your function that happened to be in a context where a constant-expression was required.

You never promised that the function was usable in a constant-expression, and without constexpr there would be no way of providing such promise.


So, why do we need constexpr?

The primary usage of constexpr is to declare intent.

If an entity isn't marked as constexpr - it was never intended to be used in a constant-expression; and even if it is, we rely on the compiler to diagnose such context (because it disregards our intent).

10
  • 32
    This is probably the correct answer, since recent changes in C++14 and C++17 allow a much wider range of the language to be used in constexpr expressions. In other words, pretty much anything can be annotated constexpr (maybe one day it will simply just go away because of this?), and unless one has a criterion of when to use constexpr or not, pretty much all of the code will be written as such.
    – alecov
    Jul 29, 2016 at 16:11
  • 6
    @alecov Definitly not everything... I/O, syscall and dynamic memory allocation definitly cann't be marked as constexpr Besides, not everything should be constexpr.
    – JiaHao Xu
    Oct 8, 2018 at 7:52
  • 1
    @alecov Some functions are meant to be executed at runtime and is meaningless to do this at compile-time.
    – JiaHao Xu
    Oct 8, 2018 at 7:53
  • 3
    I also like this answer the best. Compile time evaluation is a neat optimization, but what you really get from constexpr is a guarantee of some kind of behavior. Just like const does. Mar 14, 2019 at 10:09
  • 1
    What compiler allows this constexpr-less version of int f (int n) { return n > 0 ? n + f (n-1) : n;} T arr[f(10)]; I can't get this to compile anywhere?
    – Jer
    Apr 15, 2020 at 18:15
95

Take std::numeric_limits<T>::max(): for whatever reason, this is a method. constexpr would be beneficial here.

Another example: you want to declare a C-array (or a std::array) that is as big as another array. The way to do this at the moment is like so:

int x[10];
int y[sizeof x / sizeof x[0]];

But wouldn’t it be better to be able to write:

int y[size_of(x)];

Thanks to constexpr, you can:

template <typename T, size_t N>
constexpr size_t size_of(T (&)[N]) {
    return N;
}
13
  • 23
    @Kos: No. It would return a runtime value. constexpr forces the complier to make the function return a compile-time value (if it can).
    – deft_code
    Jan 20, 2011 at 16:00
  • 16
    @Kos: without the constexpr it cannot be used in an array size declaration, nor as a template argument, regardless of whether the result of the function call is a compile-time constant or not. These two are basically the only use-cases for constexpr but at least the template argument use-case is kind of important. Jan 20, 2011 at 16:11
  • 2
    "for whatever reason, this is a method": The reason is that there are only compile time integers in C++03, but no other compile time types, so only a method can work for arbitrary types prior to C++11. Aug 6, 2012 at 10:38
  • 6
    @LwCui No, it’s not “ok”: GCC is just lax by default about certain things. Use the -pedantic option and it will be flagged as an error. Aug 17, 2016 at 12:53
  • 2
    @SexyBeast not sure what you mean? int size is know at compile time, the constant 10 is known at compile time, so the array size is also known at compile time, nothing is "called" at run time
    – paulm
    Nov 18, 2016 at 11:56
20

constexpr functions are really nice and a great addition to c++. However, you are right in that most of the problems it solves can be inelegantly worked around with macros.

However, one of the uses of constexpr has no C++03 equivalent, typed constants.

// This is bad for obvious reasons.
#define ONE 1;

// This works most of the time but isn't fully typed.
enum { TWO = 2 };

// This doesn't compile
enum { pi = 3.1415f };

// This is a file local lvalue masquerading as a global
// rvalue.  It works most of the time.  But May subtly break
// with static initialization order issues, eg pi = 0 for some files.
static const float pi = 3.1415f;

// This is a true constant rvalue
constexpr float pi = 3.1415f;

// Haven't you always wanted to do this?
// constexpr std::string awesome = "oh yeah!!!";
// UPDATE: sadly std::string lacks a constexpr ctor

struct A
{
   static const int four = 4;
   static const int five = 5;
   constexpr int six = 6;
};

int main()
{
   &A::four; // linker error
   &A::six; // compiler error

   // EXTREMELY subtle linker error
   int i = rand()? A::four: A::five;
   // It not safe use static const class variables with the ternary operator!
}

//Adding this to any cpp file would fix the linker error.
//int A::four;
//int A::six;
20
  • 13
    Could you please clarify that "EXTREMELY subtle linker error"? Or at least provide a pointer to a clarification?
    – enobayram
    Jul 24, 2013 at 14:34
  • 4
    @enobayram, The ternary operator takes the address of the operands. That is not obvious from the code. Everything compiles fine, but the link fails because the address of four doesn't resolve. I had to really dig to figure who was taking the address of my static const variable.
    – deft_code
    Jul 24, 2013 at 19:48
  • 26
    "This is bad for obvious reasons": the most obvious reason being the semicolon, right?
    – TonyK
    Jul 28, 2013 at 20:50
  • 4
    The "EXTREMELY subtle linker error" has me completely puzzled. Neither four nor five are in scope.
    – Steven Lu
    Aug 30, 2013 at 23:30
  • 3
    see also the new enum class type, it fixes some of the enum issues.
    – ninMonkey
    Sep 5, 2013 at 2:53
15

From what I've read, the need for constexpr comes from an issue in metaprogramming. Trait classes may have constants represented as functions, think: numeric_limits::max(). With constexpr, those types of functions can be used in metaprogramming, or as array bounds, etc etc.

Another example off of the top of my head would be that for class interfaces, you may want derived types define their own constants for some operation.

Edit:

After poking around on SO, it looks like others have come up with some examples of what might be possible with constexprs.

4
  • "To be part of an interface you've got to be a function"? Jan 20, 2011 at 14:24
  • Now that I can see the usefulness of this, I'm a little more excited about C++ 0x. It seems a well thought out thing. I knew they must be. Those language standard uber-geeks seldom do random things.
    – Warren P
    Jan 20, 2011 at 21:24
  • I'm way more excited about lambdas, the threading model, initializer_list, rvalue references, variadic templates, the new bind overloads... there's quite a bit to look forward to.
    – luke
    Jan 20, 2011 at 23:29
  • 1
    Oh yeah, but I already understand lambdas/closures in several other languges. constexpr is more specifically useful in a compiler with a powerful compile-time expression evaluation system. C++ really has no peers in that domain. (that's a strong praise for C++11, IMHO)
    – Warren P
    Nov 10, 2011 at 16:17
11

From Stroustrup's speech at "Going Native 2012":

template<int M, int K, int S> struct Unit { // a unit in the MKS system
       enum { m=M, kg=K, s=S };
};

template<typename Unit> // a magnitude with a unit 
struct Value {
       double val;   // the magnitude 
       explicit Value(double d) : val(d) {} // construct a Value from a double 
};

using Speed = Value<Unit<1,0,-1>>;  // meters/second type
using Acceleration = Value<Unit<1,0,-2>>;  // meters/second/second type
using Second = Unit<0,0,1>;  // unit: sec
using Second2 = Unit<0,0,2>; // unit: second*second 

constexpr Value<Second> operator"" s(long double d)
   // a f-p literal suffixed by ‘s’
{
  return Value<Second> (d);  
}   

constexpr Value<Second2> operator"" s2(long double d)
  // a f-p literal  suffixed by ‘s2’ 
{
  return Value<Second2> (d); 
}

Speed sp1 = 100m/9.8s; // very fast for a human 
Speed sp2 = 100m/9.8s2; // error (m/s2 is acceleration)  
Speed sp3 = 100/9.8s; // error (speed is m/s and 100 has no unit) 
Acceleration acc = sp1/0.5s; // too fast for a human
5
  • 2
    This example can also be found in Stroustrup's paper Software Development for Infrastructure. Jul 24, 2013 at 7:29
  • clang-3.3: error: constexpr function's return type 'Value<Second>' is not a literal type
    – Mitja
    Aug 26, 2013 at 6:23
  • This is nice but who puts literals in code like this. Having your compiler "check your units" for you would make sense if you were writing an interactive calculator.
    – bobobobo
    Dec 16, 2013 at 4:02
  • 7
    @bobobobo or if you were writing navigation software for the Mars Climate Orbiter, maybe :) Jan 18, 2016 at 3:40
  • 1
    To make it compile - 1. Use underscore in the literal suffixes. 2. add operator ""_m for 100_m. 3. use 100.0_m, or add an overload that accepts unsigned long long. 4. Declare the Value constructor constexpr. 5. Add corresponding operator / to the Value class like this: constexpr auto operator / (const Value<Y>& other) const { return Value<Unit<TheUnit::m - Value<Y>::TheUnit::m, TheUnit::kg - Value<Y>::TheUnit::kg, TheUnit::s - Value<Y>::TheUnit::s>>(val / other.val); }. Where TheUnit is typedef for Unit added inside of the Value class.
    – 0kcats
    Dec 9, 2016 at 18:44
9

Another use (not yet mentioned) is constexpr constructors. This allows creating compile time constants which don't have to be initialized during runtime.

const std::complex<double> meaning_of_imagination(0, 42); 

Pair that with user defined literals and you have full support for literal user defined classes.

3.14D + 42_i;
8

Have just started switching over a project to c++11 and came across a perfectly good situation for constexpr which cleans up alternative methods of performing the same operation. The key point here is that you can only place the function into the array size declaration when it is declared constexpr. There are a number of situations where I can see this being very useful moving forward with the area of code that I am involved in.

constexpr size_t GetMaxIPV4StringLength()
{
    return ( sizeof( "255.255.255.255" ) );
}

void SomeIPFunction()
{
    char szIPAddress[ GetMaxIPV4StringLength() ];
    SomeIPGetFunction( szIPAddress );
}
3
  • 4
    This could equally be written: const size_t MaxIPV4StringLength = sizeof("255.255.255.255"); Jan 16, 2017 at 12:02
  • static inline constexpr const auto probably is better.
    – JiaHao Xu
    Oct 8, 2018 at 7:54
  • 1
    @JiaHaoXu: constexpr implies const and inline; static isn't implied, so adding that would change visibility. Apr 20, 2022 at 13:09
7

There used to be a pattern with metaprogramming:

template<unsigned T>
struct Fact {
    enum Enum {
        VALUE = Fact<T-1>*T;
    };
};

template<>
struct Fact<1u> {
    enum Enum {
        VALUE = 1;
    };
};

// Fact<10>::VALUE is known be a compile-time constant

I believe constexpr was introduced to let you write such constructs without the need for templates and weird constructs with specialization, SFINAE and stuff - but exactly like you'd write a run-time function, but with the guarantee that the result will be determined in compile-time.

However, note that:

int fact(unsigned n) {
    if (n==1) return 1;
    return fact(n-1)*n;
}

int main() {
    return fact(10);
}

Compile this with g++ -O3 and you'll see that fact(10) is indeed evaulated at compile-time!

An VLA-aware compiler (so a C compiler in C99 mode or C++ compiler with C99 extensions) may even allow you to do:

int main() {
    int tab[fact(10)];
    int tab2[std::max(20,30)];
}

But that it's non-standard C++ at the moment - constexpr looks like a way to combat this (even without VLA, in the above case). And there's still the problem of the need to have "formal" constant expressions as template arguments.

4
  • The fact function is not evaluated at compile-time. It needs to be constexpr and must have only one return statement.
    – Sumant
    Jul 19, 2011 at 17:08
  • 1
    @Sumant: You are right that it doesn't have to be evaluated at compile-time, but it is! I was referring to what really happens in compilers. Compile it on recent GCC, see resulting asm and check for yourself if you don't believe me!
    – Kos
    Jul 19, 2011 at 18:50
  • Try to add std::array<int, fact(2)> and you'll see that fact() is not evaluated at compile-time. It's just the GCC optimizer doing a good job.
    – user283145
    Feb 15, 2012 at 14:12
  • 2
    That's what I said... am I really that unclear? See the last paragraph
    – Kos
    Feb 15, 2012 at 18:46
4

All of the other answers are great, I just want to give a cool example of one thing you can do with constexpr that is amazing. See-Phit (https://github.com/rep-movsd/see-phit/blob/master/seephit.h) is a compile time HTML parser and template engine. This means you can put HTML in and get out a tree that is able to be manipulated. Having the parsing done at compile time can give you a bit of extra performance.

From the github page example:

#include <iostream>
#include "seephit.h"
using namespace std;



int main()
{
  constexpr auto parser =
    R"*(
    <span >
    <p  color="red" height='10' >{{name}} is a {{profession}} in {{city}}</p  >
    </span>
    )*"_html;

  spt::tree spt_tree(parser);

  spt::template_dict dct;
  dct["name"] = "Mary";
  dct["profession"] = "doctor";
  dct["city"] = "London";

  spt_tree.root.render(cerr, dct);
  cerr << endl;

  dct["city"] = "New York";
  dct["name"] = "John";
  dct["profession"] = "janitor";

  spt_tree.root.render(cerr, dct);
  cerr << endl;
}
0
2

A lot of the responses here seem to have things a bit backwards, and/or are saying the quiet part loud and the loud part quiet. The one key thing to know about constexpr is this:

// This guarantees only that the value of "MeaningOfLife" can not be changed
// from the value calculated on this line by "complex_initialization()"
// (unless you cast away the const of course, don't do that).
// Critically here, everything happens at *runtime*.
const int MeaningOfLife = complex_initialization(1234, 5678, "hello");
// This guarantees that "MeaningOfLife" is fully evaluated and "initialized"
// *at compile time*.  If that is not possible due to complex_initialization()
// not being evaluatable at compile time, the compiler is required to abort
// compilation of the program.
// Critically here, to put a fine point on it, everything happens at
// *compile time*, guaranteed.  There won't be a runtime call to
// complex_initialization() at all in the final program.
constexpr int MeaningOfLife = complex_initialization(1234, 5678, "hello");

Note that it's the constexpr-ness of the left-hand side which forces the guarantees which give constexpr its reason to exist. It is up to you to make sure the right-hand side can in fact be evaluated at compile time of course, and importantly, just declaring a function constexpr does not in and of itself do that.

So the answer to your question is that you should declare a variable constexpr when you need or want its initialization (everything happening on the right-hand side) to be forced to either happen entirely at compile time or break the build.

1

Your basic example serves he same argument as that of constants themselves. Why use

static const int x = 5;
int arr[x];

over

int arr[5];

Because it's way more maintainable. Using constexpr is much, much faster to write and read than existing metaprogramming techniques.

0

It can enable some new optimisations. const traditionally is a hint for the type system, and cannot be used for optimisation (e.g. a const member function can const_cast and modify the object anyway, legally, so const cannot be trusted for optimisation).

constexpr means the expression really is constant, provided the inputs to the function are const. Consider:

class MyInterface {
public:
    int GetNumber() const = 0;
};

If this is exposed in some other module, the compiler can't trust that GetNumber() won't return different values each time it's called - even consecutively with no non-const calls in between - because const could have been cast away in the implementation. (Obviously any programmer who did this ought to be shot, but the language permits it, therefore the compiler must abide by the rules.)

Adding constexpr:

class MyInterface {
public:
    constexpr int GetNumber() const = 0;
};

The compiler can now apply an optimisation where the return value of GetNumber() is cached and eliminate additional calls to GetNumber(), because constexpr is a stronger guarantee that the return value won't change.

5
  • Actually const can be used in optimisation... It's undefined behaviour to modify a value defined const even after a const_cast IIRC. I'd expect it to be consistent for const member functions, but I'd need to check that with the standard. This would mean that the compiler can safely do optimisations there.
    – Kos
    Jan 20, 2011 at 23:27
  • 1
    @Warren: it doesn't matter if the optimization is actually done, it's just allowed. @Kos: it's a little-known subtlety that if the original object was not declared const (int x vs. const int x), then it is safe to modify it by const_cast -ing away const on a pointer/reference to it. Otherwise, const_cast would always invoke undefined behavior, and be useless :) In this case, the compiler has no information about the const-ness of the original object, so it can't tell. Jan 21, 2011 at 1:59
  • @Kos I don't think const_cast is the only issue here. The const method is allowed to read and even modify a global variable. Conversely, someone from anpther thread could also modify the const object between the calls.
    – enobayram
    Jul 24, 2013 at 14:47
  • 2
    The "= 0" isn't valid here and should be removed. I'd do it myself, but I'm not sure that's in conformance with SO protocol. Jan 24, 2014 at 3:15
  • 1
    Both examples are invalid: the first one (int GetNumber() const = 0;) should declare the GetNumber() method virtual. The second (constexpr int GetNumber() const = 0;) isn't valid because the pure specifier (= 0) implies the method to be virtual, but constexpr's must not be virtual (ref: en.cppreference.com/w/cpp/language/constexpr)
    – stj
    Mar 4, 2016 at 10:22
-4

It's useful for something like

// constants:
const int MeaningOfLife = 42;

// constexpr-function:
constexpr int MeaningOfLife () { return 42; }

int some_arr[MeaningOfLife()];

Tie this in with a traits class or the like and it becomes quite useful.

3
  • 4
    In your example it offers zero advantage over a plain constant, so it doesn't really answer the question.
    – jalf
    Jan 20, 2011 at 14:48
  • This is a contrived example, imagine if MeaningOfLife() gets its value from somewhere else, say another function or a #define or series therof. You may not know what it returns, it may be library code. Other examples, imagine an immutable container that has a constexpr size() method. You can now do int arr[container.size()];
    – plivesey
    Jan 21, 2011 at 11:49
  • 2
    @plivesey can you please edit your answer with a better example then.
    – Mukesh
    Sep 20, 2017 at 6:14
-4

When to use constexpr:

  1. whenever there is a compile time constant.
1
  • While I agree with you, this answer doesn't explain why constexpr should be preferred over preprocessor macros or const.
    – Sneftel
    Jun 13, 2019 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.