Append items to std::initializer_list at compile time

Viewed 196

Is it possible to append items to a std::initializer_list at compile time?

I am writing a struct to try to collect in a std::initializer_list a bunch of const char* names coming from a 3rd party lib which are associated to some specific types from the same lib as the names, but disconnected codewise (e.g. the names are not part of the types, they are not even in the same namespace).

This is the struct I am trying to write:

template < typename T, typename... Args >
struct S : S< T >, S< Args... >
{
    static auto
    get_names( )
    {
        // concatenation should go here
        // return S< T >::get_names() + S< Args >::get_names()...
        // or something similar
    }
};

template <>
struct S< lib::types::SpecificTypeA >
{
    static auto
    get_names( )
    {
        // the name is a const char * coming from the same 3rd party lib
        return { lib::names::name_of_specific_type_a };
    } 
};

I am using C++17.

1 Answers

As far as I know, you can't construct any object at compile time in C++.

No object can be constructed before the program starts.

We usually construct these objects at the beginning of main(). It doesn't affect the performance.

Related