std::apply and constant expression?

946 Views Asked by At

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?

3

There are 3 best solutions below

0
On BEST ANSWER

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.

Another approach is you can pass std::integral_constant<T, t> to the lambda through some mechanism, like my indexer. Then converting to T is constexpr even tho the variable is not. This does not help you with "hello" to a sequence.

14
On

What you want can be done without std::apply:

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

template <std::size_t N, class = std::make_index_sequence<N>>
struct iterate;

template <std::size_t N, std::size_t... Is>
struct iterate<N, std::index_sequence<Is...>> {
    template <class Lambda>
    constexpr auto operator()(Lambda lambda) {
        return lambda(std::integral_constant<std::size_t, Is>{}...);
    }
};

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

[live demo]

0
On

Function parameters cannot be labeled constexpr. As such, you cannot use them in places that require constant expressions, like non-type template arguments.

To do the kind of thing you're trying to do would require some kind of compile-time string processing, based around template arguments.