2

I can't get this to work. I want it to check for fundamental types but also for pointers of fundamental types:

template<typename T> struct non_void_fundamental : 
            boost::integral_constant<bool, 
                (boost::is_fundamental<T>::value && !boost::is_same<T, void>::value)
                || (boost::is_fundamental<*T>::value && !boost::is_same<*T, void>::value)
        >
        { };

Maybe some one can help me point me in the right direction.

Edit: It is especially the 4th line which doesn't do what I want, the rest works fine.

Edit 2: The point would be that it generates the following output in the following example:

int* p = new int(23);
cout << non_void_fundamental<double>::value << endl // true
cout << non_void_fundamental<some_class>::value << endl // false
cout << non_void_fundamental<p>::value << endl // true

Edit 3: Thanks to Kerrek SB, I have this know, but it is producing some errors.

template<typename T> struct non_void_fundamental : 
            boost::integral_constant<bool, 
                (boost::is_fundamental<T>::value && !boost::is_same<T, void>::value)
                || (boost::is_pointer<T>::value &&     boost::is_fundamental<boost::remove_pointer<T>::type>::value && !boost::is_same<boost::remove_pointer<T>::type, void>::value)
            >
        { };

Erros:

FILE:99:61: error: type/value mismatch at argument 1 in temp
late parameter list for 'template<class T> struct boost::is_fundamental'
FILE:99:61: error:   expected a type, got 'boost::remove_poi
nter<T>::type'
FILE:99:125: error: type/value mismatch at argument 1 in tem
plate parameter list for 'template<class T, class U> struct boost::is_same'
FILE:99:125: error:   expected a type, got 'boost::remove_po
inter<T>::type'
4

1 に答える 1

3

You got it the wrong way round, completely. Let's just concentrate on "T is a pointer to a fundamental type": That is:

Now just put those all together. In pseudo-code:

value = (is_fundamental<T>::value && !is_void<T>::value) ||
        (is_pointer<T>::value && is_fundamental<remove_pointer<T>::type>::value)

In real code, Boost version:

#include <boost/type_traits.hpp>

template <typename T>
struct my_fundamental
{
    static bool const value =
      (boost::is_fundamental<T>::value && ! boost::is_void<T>::value) ||
      (boost::is_pointer<T>::value &&
       boost::is_fundamental<typename boost::remove_pointer<T>::type>::value);
};

In C++11, change the include to <type_traits> and boost:: to std::.

于 2012-11-11T13:42:34.077 に答える