Accepting std::array of char of any size as non type template parameter

354 views Asked by At

This is probably a weird use case, but I am trying to hack around the fact string literals can not be used as arguments to templates using std::array<char, N> as non template type parameter.

This works but with extreme limitation that all strings must be of same length(I could use MAX_STR_LEN=100 or whatever and make all arrays that size, but that feels ugly...).

Is there a way to make this code work so that different size std::arrays can be accepted as template parameter?

#include <iostream>
#include <array>
#include <tuple>
#include <boost/mp11/algorithm.hpp>
#include <boost/mp11/tuple.hpp>
// I wish that this 6 is not fixed... but IDK how to fix it, maybe concept(IDK if concepts can be used as "types" on NTTP.
template <typename Type, std::array<char, 6> val_val>
struct TypeToValues
{
    using type = Type;
    static constexpr const char* val = val_val.data();
};

template <std::size_t Sz, std::size_t... Is>
constexpr std::array<char, Sz>
    arrayify(const char (&arr)[Sz], std::index_sequence<Is...>)
{
    return {{arr[Is]...}};
}

template <std::size_t Sz>
constexpr std::array<char, Sz> arrayify(const char (&arr)[Sz])
{
    return arrayify(arr, std::make_index_sequence<Sz>());
}
struct HelloType{

};
struct YoloType{

};
int main(){
    std::tuple<
    TypeToValues<HelloType, arrayify("Hello")>,
    TypeToValues<YoloType, arrayify("Yolo!")>> mapping;
    boost::mp11::tuple_for_each(mapping, []<typename T>(const T&){
        if constexpr(std::is_same_v<typename T::type, HelloType>){
            std::cout << "HelloType says: " << T::val << std::endl;;
        } 
        if constexpr(std::is_same_v<typename T::type, YoloType>){
            std::cout << "YoloType says: " << T::val << std::endl;;
        }
    });

}
1

There are 1 answers

1
ecatmur On BEST ANSWER

Sure, why not use a requires requires clause?

template <typename Type, auto val_val>
    requires requires { { val_val.data() } -> std::same_as<char const*>; }
struct TypeToValues
{
    // ...

Example.

You could also write a constraint that only specifically std::array<char, N> satisfy:

template<class> constexpr bool is_array_of_char_v = false;
template<unsigned N> constexpr bool is_array_of_char_v<std::array<char, N>> = true;
template<class T> concept ArrayOfChar = requires { is_array_of_char_v<T>; };

template <typename Type, ArrayOfChar auto val_val>
struct TypeToValues
{
    // ...

But that feels excessively restrictive; you'll want to accept static string types in future.