Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The simplest way is to create a type-traits object. Basically, you create an object (let's call it is_fundamental&lt;T&gt;) that is parameterized by the type and that inherits from boost::type_traits::no_type by default; then you specialize the object on all the fundamental types, making that specialization inherit from boost::type_traits::yes_type. So then you can use is_fundamental&lt;T&gt;::value as a boolean which will tell you if the type T is or is not a fundamental type. In most cases, you really don't need to know if a type is or is not fundamental, and when you do, it almost always involves templates, anyway, so might as well do it that way.</p> <p>I should also point out that Boost already defines <a href="http://www.boost.org/doc/libs/1_42_0/libs/type_traits/doc/html/boost_typetraits/reference/is_fundamental.html" rel="nofollow noreferrer">boost::type_traits::is_fundamental</a> which does what you want. You can see in <a href="http://www.boost.org/doc/libs/1_42_0/boost/type_traits/is_fundamental.hpp" rel="nofollow noreferrer">is_fundamental.hpp</a> that they define it in terms of their other type-traits objects; a type is fundamental if it is a builtin arithmetic type or if it is the type "void" (which is also considered to be fundamental). Trying to figure things out from Boost can be kind of confusing but a simplification is:</p> <pre><code>template&lt;typename T, T VAL&gt; struct constant_value { static const T value = VAL; }; typedef constant_value&lt;bool,true&gt; yes_type; typedef constant_value&lt;bool,false&gt; no_type; template&lt;typename T&gt; struct is_fundamental : public no_type{}; // Create a macro for convenience #define DECLARE_FUNDAMENTAL(X) \ template&lt;&gt; struct is_fundamental&lt;X&gt; : public yes_type{} // Specialize for all fundamental types DECLARE_FUNDAMENTAL(void); DECLARE_FUNDAMENTAL(bool); DECLARE_FUNDAMENTAL(signed char); DECLARE_FUNDAMENTAL(unsigned char); // ... lots more similar specializations ... DECLARE_FUNDAMENTAL(wchar_t); DECLARE_FUNDAMENTAL(float); DECLARE_FUNDAMENTAL(double); DECLARE_FUNDAMENTAL(long double); // Prevent this macro from polluting everything else... #undef DECLARE_FUNDAMENTAL </code></pre> <p>That is essentially what it takes to create such a type-traits object. Note that one can maliciously specialize the type-trait to be true for a non-fundamental type, although that is the case for most things, anyway.</p> <p>You can then use the above to create a more function-looking thing. For example, using the boost::type_traits::is_fundamental class, you could create the following:</p> <pre><code>template&lt;typename T&gt; bool isFundametal(const T&amp;) { return boost::type_traits::is_fundamental&lt;T&gt;::value; } </code></pre> <p>Because the template specialization can be deduced from the parameters, you can invoke this isFundamental function without explicitly specifying the type. For example, if you write isFundamental(5) it will implicitly invoke isFundamental&lt;int&gt;(5), which will return true. Note, though, that if you create such a function, it won't allow you to test for void. You could create a function that took no parameters for such a case, but then the type wouldn't be deduced, and so it would be no prettier than simply using boost::type_traits::is_fundamenta&lt;T&gt;::value, and so one might as well just use it in that case.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload