c++ - Boost::type_traits 基本类型和指针

标签 c++ pointers 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)
        >
        { };

也许有人可以帮我指出正确的方向。

编辑:尤其是第四行没有达到我想要的效果,其余的都可以正常工作。

编辑 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'

最佳答案

你完全搞错了。让我们只关注“T是一个指向基本类型的指针”:即:

现在把它们放在一起。在伪代码中:

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

在实际代码中,Boost版本:

#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:: .

关于c++ - Boost::type_traits 基本类型和指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13331585/

相关文章:

c++ - 在 C++ 中使用静态非成员变量与非静态成员变量

c++ - libstdc++.so.6 : version GLIBCXX_3. 4.20 未找到

c++ - 使用 WinAPI 的多个读取器/单个写入器锁定

c - 使用 malloc 为结构体分配内存

c# - c# 中带有列表的动态类型

C - 'file' 错误的冲突类型

c++ - 未定义对 `inflate' 的引用

c - 格式 '_' 需要类型为 '__' 的参数,但参数 2 的类型为 '__' 。 C编程,我是初学者

c - 何时对结构使用指针与直接成员

sql - 行构造函数有什么用?