20

In C++0x, one can create a constexpr std::tuple, e.g. like

#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);

One also can query a std::tuple at runtime, e.g. via

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

But it is not possible to query it at compile time, e.g.,

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

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

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

And if not, is there a conceptual reason why one is not supposed to query it?

(I am aware of avoiding using std::tuple, e.g., by using boost::mpl or boost::fusion instead, but somehow it sounds wrong not to use the tuple class in the new standard...).

By the way, does anybody 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);

not?

Thanks a lot in advance! - lars

Lars
  • 2,616
  • 3
  • 21
  • 18
  • In n3225.pdf, only the default constructor is marked constexpr. Perhaps it is just too early to use this feature? – UncleBens Feb 23 '11 at 15:09

4 Answers4

13

std::get is not marked constexpr, so you cannot use it to retrieve the values from a tuple in a constexpr context, even if that tuple is itself constexpr.

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

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
  • Thanks. So your answer is "no, it is not possible." I had a look into meanwhile and totally agree. This leaves the question: why cannot we query std::tuples at compile time? But evtl. this question is better discussed in the broader scope of my [question on constexpr of std::forward](http://stackoverflow.com/questions/5098069/why-discards-stdforward-constexpr-ness). – Lars Feb 23 '11 at 23:04
  • 9
    [N3305](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3305.html) proposes to add constexpr for `tuple::get` – Dave Abrahams Sep 12 '12 at 15:29
3

Now, std::get<> is a constexpr function. The following code compiles for me if I use gcc c++ 11 or above.

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

Furthermore, you can generate a list of numbers at compile time by using make_index_sequence (c++14 or above) and access the tuple.

constexpr auto size = std::tuple_size<decltype(tup)>::value;
for_sequence(std::make_index_sequence<size>{}, [&](auto i){
        constexpr auto property = std::get<i>(tup);
        std::cout<<property<<std::endl;
});

template <typename T, T... S, typename F>
constexpr void for_sequence(std::integer_sequence<T, S...>, F&& f) {
    using unpack_t = int[];
    (void)unpack_t{(static_cast<void>(f(std::integral_constant<T, S>{})), 0)..., 0};
}
sayem siam
  • 1,281
  • 3
  • 13
  • 26
0

This issue was fixed in c++17. But if you want to pass tuple into the constexpr function as a parameter, i would strongly recommend to read this post https://mpark.github.io/programming/2017/05/26/constexpr-function-parameters/

klockvud
  • 99
  • 1
  • 5
-2

I have not yet worked with C++0x, but it seems to me that std::get() is a function, rather than expression the compiler can directly interpret. As such, it has no meaning except at runtime, after the function itself has been compiled.

  • 1
    C++0x adds the keyword `constexpr` that actually forces the compiler to evaluate a function at compile time, i.e. with `constexpr int add(int x, int y) { return x+y;}` you can say `constexpr int i = add(7, 13);` and the value of i is available at compile time, e.g., in `static_assert(i == 20, "i correct");`. -- So my question is why is `std::get` not constexpr ? – Lars Feb 23 '11 at 21:55
  • The idea of `constexpr` is that the compiler can be told to calculate the result of a function call given that arguments are also const-expressions. – UncleBens Feb 23 '11 at 21:57
  • That's not my reading of the references I've found. My reading is that a constexpr can *define* an inline function returning a constant expression (replacing macros), but it can only *call* a function if that function is itself constexpr. – Michael Scott Shappe Feb 23 '11 at 21:58
  • @Lars -- I suppose my guess would be, because std::get(arg) is going to be used in situations where arg is not itself going to be a constexpr. – Michael Scott Shappe Feb 23 '11 at 22:06
  • 1
    this would not be a problem either as constexpr will be discarded silently when called with non-constexpr arguments. – Lars Feb 23 '11 at 23:05