2012-11-11 53 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