Cu2S Cu2S - 1 year ago 98
C++ Question

std::apply and constant expression?

I tried code below in Wandbox:

#include <array>
#include <iostream>
#include <tuple>
#include <typeinfo>
#include <functional>
#include <utility>


int main()
{
constexpr std::array<const char, 10> str{"123456789"};
constexpr auto foo = std::apply([](auto... args) constexpr { std::integer_sequence<char, args...>{}; } , str);
std::cout << typeid(foo).name();
}


and the compiler told me that
args...
are not constant expression.
What's wrong?

Answer Source

All constexpr functions must be valid both constexpr and not, even if marked constexpr.

There is a proposal for a constexpr literal that passed characters as non type template parameters. Then "hello"_bob could expand directly to a parameter pack.