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

4

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

Uttermost answered 23/2, 2011 at 5:34 Comment(1)
In n3225.pdf, only the default constructor is marked constexpr. Perhaps it is just too early to use this feature?Castello
G
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.

Gondi answered 23/2, 2011 at 22:31 Comment(2)
Thanks. So your answer is "no, it is not possible." I had a look into <tuple> 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.Uttermost
N3305 proposes to add constexpr for tuple::getBecerra
F
4

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};
}
Footy answered 28/1, 2020 at 17:35 Comment(0)
B
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/

Bouchier answered 28/3, 2022 at 13:11 Comment(0)
G
-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.

Gerrilee answered 23/2, 2011 at 21:38 Comment(5)
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 ?Uttermost
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.Castello
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.Gerrilee
@Uttermost -- I suppose my guess would be, because std::get<T>(arg) is going to be used in situations where arg is not itself going to be a constexpr.Gerrilee
this would not be a problem either as constexpr will be discarded silently when called with non-constexpr arguments.Uttermost

© 2022 - 2024 — McMap. All rights reserved.