How to request constexpr std :: tuple at compile time?

In C ++ 0x, you can create constexpr std :: tuple, for example. as

#include <tuple> constexpr int i = 10; constexpr float f = 2.4f; constexpr double d = -10.4; constexpr std::tuple<int, float, double> tup(i, f, d); 

You can also request std :: tuple at run time, for example. via

 int i2 = std::get<0>(tup); 

But it is not possible to request it at compile time, for example,

 constexpr int i2 = std::get<0>(tup); 

will cause a compilation error (at least with the latest g ++ snapshot 2011-02-19).

Is there any other way to request constexpr std :: tuple at compile time?

And if not, is there a conceptual reason why no one should request it?

(I know about avoiding using std :: tuple, for example, using boost :: mpl or boost :: fusion instead, but somehow it seems wrong to not use the tuple class in the new standard ...).

By the way, does anyone know why

  constexpr std::tuple<int, float, double> tup(i, f, d); 

compiles fine but

  constexpr std::tuple<int, float, double> tup(10, 2.4f, -10.4); 

no?

Thank you very much! - lars

+18
c ++ c ++ 11 const tuples
Feb 23 2018-11-11T00:
source share
2 answers

std::get not marked with constexpr , so you cannot use it to extract values ​​from tuple in the context of constexpr , even if this tuple itself is constexpr .

Unfortunately, the implementation of std::tuple opaque, so you also cannot write your own accessors.

+12
Feb 23 2018-11-23T00:
source share

I have not worked with C ++ 0x yet, but it seems to me that std :: get () is a function, not an expression that the compiler can interpret. Thus, it makes no sense, except at run time, after the function itself has been compiled.

-2
Feb 23 2018-11-23T00:
source share



All Articles