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
782 views
in Technique[技术] by (71.8m points)

c++ - Template specialization for enum

Is it possible to specialize a templatized method for enums?

Something like (the invalid code below):

template <typename T>
void f(T value);

template <>
void f<enum T>(T value);

In the case it's not possible, then supposing I have specializations for a number of types, like int, unsigned int, long long, unsigned long long, etc, then which of the specializations an enum value will use?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You can use std::enable_if with std::is_enum from <type_traits> to accomplish this.

In an answer to one of my questions, litb posted a very detailed and well-written explanation of how this can be done with the Boost equivalents.


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

2.1m questions

2.1m answers

60 comments

56.9k users

...