Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

c++ - Can I get typeid from variadic template types without passing args?

I have a system that saves typeid.name() data in a vector from various class instances, and another system that creates bitset signatures for required combinations of classes. This signature system has no need to any class instances passed in.

I know how to get typeid.name() from non-variadic template

template<class T>
const char* getTypeName() const
{
    return typeid(T).name();
}

And I figured how to get typeid.name() from variadic template with args

template<class... T> 
void getTypeNames(T... args)
{
    std::vector<const char*> typeNames;
    (typeNames.push_back(typeid(std::forward<T>(args) ).name()), ...  );

    return typeNames;
}

But what I would really like to do is something like this:

template<class... T> 
void getTypeNames()
{
    std::vector<const char*> typeNames;
    (typeNames.push_back(typeid(std::forward<T>()).name()), ...  );

    return typeNames;
}

So is it possible to get typeid like this or do I need to find another way?

PS. first time posting to stackoverflow so sorry if question is too vague or not clear enough.

question from:https://stackoverflow.com/questions/65843593/can-i-get-typeid-from-variadic-template-types-without-passing-args

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I believe this is what you want:

template<class... T> 
std::vector<const char*> getTypeNames() {
    return {typeid(T).name()...};
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...