Lars
Lars

Reputation: 2636

How to query a constexpr std::tuple at compile time?

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

Upvotes: 20

Views: 8142

Answers (4)

watislaf
watislaf

Reputation: 309

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/

Upvotes: 0

sayem siam
sayem siam

Reputation: 1311

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};
}

Upvotes: 4

Anthony Williams
Anthony Williams

Reputation: 68611

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.

Upvotes: 13

Michael Scott Shappe
Michael Scott Shappe

Reputation: 360

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.

Upvotes: -2

Related Questions