Possible Duplicate:
C++ templates that accept only certain types
For example, if we want to define a template function which we can use integers, floats, doubles but not strings. Is there an easy way to do so?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The way to do this to use
std::enable_ifin some shape or form. The selector for the supported type is then used as the return type. For example:Obviously, you want to give the traits a better name than
is_supported.std::enable_ifis part of C++2011 but it is easily implemented or obtained from boost in case it isn’t available with the standard library you are using.In general, it is often unnecessary to impose explicit restrictions as the template implementation typically has implicit restrictions. However, sometimes it is helpful to disable or enable certain types.