In < Modern C++ Design >,it introduces a way to check if a type fundamental type by introducing the so called type list. but what if I don’t want to include so many loki code and just want a simple function to implement that? What is the simplest way to do that?
Share
You can use template specialization to get what you want.
In this code, if you pass in a type such as
intorchar, the compiler will use the specialization ofIsFundamentalType(given that you’ve defined the specializations for all fundamental types). Otherwise, the compiler will use the general template, as it is the case for theNonFundamentalTypeclass. The important thing is that the specialized ones have aresultmember defined astrue, while the general template also has aresultmember defined asfalse. You can then use theresultmember for theifstatement. Optimizing compilers should be able to elide theifstatement seeing that the expression reduces to a constant true/false value, so doing something like this should not impose a runtime penalty.