Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 非限定类型或指针/引用类型的特征_C++_Template Meta Programming - Fatal编程技术网

C++ 非限定类型或指针/引用类型的特征

C++ 非限定类型或指针/引用类型的特征,c++,template-meta-programming,C++,Template Meta Programming,我正在实施一项检查,以查看某一类型是否被剥夺了任何资格: template <class T> struct is_plain : std::integral_constant< bool, std::is_same_v<T, std::decay_t<T>> // * >; 模板 结构是普通的:std::整型常数< 布尔, std::是否相同* >; std::Is_-same_v中的逻辑是否相同,即检查类型是否去除了衰退会

我正在实施一项检查,以查看某一类型是否被剥夺了任何资格:

template <class T>
struct is_plain : std::integral_constant<
    bool, 
    std::is_same_v<T, std::decay_t<T>> // *
>;
模板
结构是普通的:std::整型常数<
布尔,
std::是否相同*
>;
std::Is_-same_v
中的逻辑是否相同,即检查类型是否去除了
衰退
会去除的任何东西,作为标准特征可用

如果没有,我的实现是否缺少什么?我只想确保(在静态断言中)不传递指向某个类的指针或引用

更新
正如@NathanOliver所指出的,
decay
不会删除pointerness(如果这是一个词的话),因此
decay\u t==int*
。我没有意识到这一点,因此在描述中添加的一个内容是我想从类型中删除指针

您的指针对数组不起作用。有一个非常相似的标准特征:

std::is_same_v<T, std::remove_cvref_t<T>>
std::是否相同

这有点冗长,但应该涵盖所有案例。与

template<typename T>
constexpr auto is_plain_v = !(std::is_function_v<T> || std::is_pointer_v<T> || 
                              std::is_lvalue_reference_v<T> || std::is_rvalue_reference_v<T> || 
                              std::is_array_v<T> || std::is_member_object_pointer_v<T> || 
                              std::is_member_function_pointer_v<T> || std::is_const_v<T> || 
                              std::is_volatile_v<T>);

关于你的更新,
decay
不会删除指针,所以如果t是
int*
decay\u t
也是
int*
@NathanOliver,这是我完全误解的,谢谢你指出。也许我应该改变一下描述。你希望函数类型发生什么变化,比如
int(int)
?既然它不是一个指向函数类型的指针,那么这应该被认为是简单的吗?另外,应该考虑什么?这很简单,因为没有cv限定符,还是因为它是一种可以衰减为指针的类型,并且不可复制?请注意,这一特性需要C++20支持,尽管在
std::remove_cv
std::remove_reference
方面实现起来非常简单,或者如果您的项目已经使用了boost,有
boost::remove\u cv\u ref\u t
struct Test { void foo(int) {} };

int main () 
{
    std::cout << std::boolalpha
              << is_plain_v<int> << '\n'
              << is_plain_v<const int> << '\n'
              << is_plain_v<int&> << '\n'
              << is_plain_v<int&&> << '\n'
              << is_plain_v<const int&> << '\n'
              << is_plain_v<int[2][2]> << '\n'
              << is_plain_v<int(int)> << '\n'
              << is_plain_v<int(*)(int)> << '\n'
              << is_plain_v<void (Test::*)(int)> << '\n';
}
true
false
false
false
false
false
false
false
false