C++ index of type during variadic template expansion

后端 未结 4 813
别那么骄傲
别那么骄傲 2020-12-08 21:43

I have a simple yet daunting problem I can\'t solve by myself. I have something like

template
T* create(SomeCastableType* args,         


        
4条回答
  •  无人及你
    2020-12-08 22:48

    Suppose SomeCastableType is castable to any type. Obviously what I can't get is that INDEX_OF_EXPANSION.

    Since C++14, you can do the indices trick @Xeo mentioned with the support from the standard library, by using the std::make_index_sequence helper, as follows:

    template
    T* create(SomeCastableType* p, std::index_sequence)
    {
        return new T(static_cast(p[Is])...);
    }
    
    template
    T* create(SomeCastableType* p, std::size_t num_args)
    {
        return create(p, std::make_index_sequence());
    }
    

提交回复
热议问题