Yunsheng Guo
Yunsheng Guo

Reputation: 85

Is there a way to store compile time constant in a class instance?

I was trying to see if I can make a heterogeneous type that can only contain one of several types in its lifetime(an either pattern), and I want to do this:

//strong typed (heterogeneous) container
template<typename list>
struct STC
{
    template<typename a>
    STC(const a& value) :hc_(value), index_(TMP::elem_index<a, list>::value) {}

    template<typename a>
    STC(a&& value) : hc_(value), index_(TMP::elem_index<a, list>::value) {}

    //imaginary compile time const
    constexpr size_t index()const { return index_; }

    typename TMP::index<list,index()>::type get() { return c_.get<index()>(); } 

    operator typename TMP::index<list,index()>::type ()const { return get(); }

private:
    union_magic<list> c_;
    const size_t index_;
};

where typename list is a template meta type list, TMP::elem_index is a template meta function for retriving the index of an list element, and TMP::index is a meta function for retriving element with known index.

It seems to me that there is no way to cross the line between class data member and compile time constant. Am I missing something? Is this the wrong approach? Or it's just something impossible to do in c++, and has to be resolved in runtime?

As for how do I use the container:

    void call_with(char c)
    {
        std::cout << "calling function with char type: " << c << std::endl;
    }

    void call_with(int i)
    {
        std::cout << "calling function with int type: " << i<< std::endl;
    }

    int main()
    {        
    STC<Cons<int, Cons<char, Nil>>> value(1);
    call_with(value);
    }

should display "calling function with int type: 1".

Upvotes: 2

Views: 680

Answers (1)

Edy
Edy

Reputation: 470

Would the combination of the two following answers help to get what you want?

building and accessing a list of types at compile time

How can I get the index of a type in a variadic class template?

BTW, it's not useful to store a compile-time metatype in index_, which is allocated and can only be used at run time. You'd probably define it as a static constexpr size_t kIndex instead.

Upvotes: 1

Related Questions