c++ - 为什么 std::is_copy_constructible 的行为不符合预期?
问题描述
#include <type_traits>
int main()
{
std::is_constructible_v<int&, const int&>; // false, as expected.
std::is_copy_constructible_v<int&>; // true, NOT as expected!
}
根据cppref:
如果 T 是对象或引用类型且变量定义 T obj(std::declval()...); 是格式良好的,提供等于 true 的成员常量值。在所有其他情况下,value 为 false。
std::is_copy_constructible_v<int&>
应该给出相同的结果std::is_constructible_v<int&, const int&>
;但是,clang 7.0
给出不同的结果,如上所示。
这种行为是否符合 C++ 标准?
解决方案
is_copy_constructible状态的参考是:
如果 T 不是可引用类型(即,可能是 cv 限定的 void 或具有 cv-qualifier-seq 或 ref-qualifier 的函数类型),则提供等于 false 的成员常量值。否则,提供一个等于 的成员常量值
std::is_constructible<T, const T&>::value
。
所以,这里is_copy_constructible<T>::value
是一样的std::is_constructible<T, const T&>::value
。
所以在你的情况下:
std::is_constructible<int, const int&>::value
将与 相同std::is_copy_constructible_v<int>
。
看DEMO
推荐阅读
- android - Android Project的状态栏不能变色
- python - matplotlib:如何精确定位文本?
- php - HTML/PHP:移动 Web 导航栏不起作用
- java - 在 Resteasy Reactive / Quarkus 中使用枚举作为查询参数
- python - Python 部署到 Azure 应用服务失败且没有错误
- html - Bootstrap 导航栏右对齐
- java - 从 invokedynamic 调用 Java varargs 方法
- java - RecycleView 中显示的额外空 ViewHolder 布局
- homebrew - 把 brew install openconnect 403 ,怎么安装?
- typescript - 如何遍历 TypeScipt 中的对象-正确的方法