2012-11-11 55 views
2

我无法得到这个工作。我想它来检查基本类型也为基本类型的指针:Boost :: type_traits基本类型和指针

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) 
     > 
     { }; 

也许有人可以帮我点我在正确的方向。

编辑:这是特别是第四行不做我想要的,其余的正常工作。

编辑2:点是,这产生在下面的例子中下面的输出:

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 

编辑3:由于Kerrek SB,我有此知道,但它是生产一些错误。

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) 
      > 
     { }; 

误差修改:

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' 
+0

你有什么错误? – 0x499602D2

+0

这不是关于它产生的错误。但更多关于正确的方法,这绝对不行。看我的编辑。 – Tim

+0

你确定'boost :: is_fundamental <*T>'*不是*应该是'boost :: is_fundamental ''? 'is_same <*T>' – 0x499602D2

回答

3

你理解南辕北辙,完全。让我们只专注于“T是指向一个基本类型”:即:

现在把这些放在一起。在伪码:

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

在实际代码,升压版本:

#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); 
}; 

在C++ 11,改变包括对<type_traits>boost::std::

+0

你的意思是'remove_pointer'而不是'remove_reference'我很痛苦。感谢您的回答,这似乎是正确的解决方案! – Tim

+0

@Tim:谢谢!我永远不会使用'remove_pointer',但是'remove_reference'所有的时间,所以我的手指记忆击败了我... –

+0

我得到一些错误的知道你可以看看他们(在第一篇文章中)? – Tim