lock3 / meta

122 stars 11 forks source link

Cannot use variadic valueof in constexpr function? #283

Open rbock opened 3 years ago

rbock commented 3 years ago

Hi,

I cannot seem to use variadic valueof in constexpr functions? The following does not compile:

#include <array>

struct X {
  constexpr X(...) {}
};

constexpr auto tuple_cat() {
  constexpr auto values = std::array{reflexpr(1)};
  return X{valueof(...values)};
}

int main() {}

See: https://cppx.godbolt.org/z/q4v4s1

The compiler suggests to add static. But it turns out that static variables are not allowed in constexpr functions, see https://cppx.godbolt.org/z/xeP46s

However, it seems the consteval restrictions that make the compiler ask for static might be overly strict. When I replace the constructor, the compiler notices if the arguments do not match. So I guess it already starts evaluating the variadic valueof and complains merely as an afterthought, see https://cppx.godbolt.org/z/17bbqE

Unfortunately (IMHO), the order of use and check have changed in the splice branch, hiding the hint that it might actually be possible?

Thanks,

Roland

TimPhoeniX commented 3 years ago

I'm inclined to believe that std::array iterators aren't fully compatible with variadic reification code. See https://cppx.godbolt.org/z/qrh44K. That is just a hypothesis, though.

rbock commented 3 years ago

Thanks for the hint. Good to know it works with a c-style array!

DarkArc commented 3 years ago

The pack expansion support is being reworked quite a bit in the syntax refactor branch/it effectively replaces variadic reifiers. I expect this will be fixed when that is released, though it will look something like:

constexpr auto tuple_cat() {
  constexpr auto values = std::array{reflexpr(1)};
  return X{...[< values >]...};
}
rbock commented 3 years ago

That would be great! std:array is so much more convenient to pass as parameter or return from a function :-)