c++ - 为什么 std::is_copy_constructible 的行为不如预期?

标签 c++ c++11 standards typetraits compile-time-constant

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

If T is an object or reference type and the variable definition T obj(std::declval()...); is well-formed, provides the member constant value equal to true. In all other cases, value is false.

std::is_copy_constructible_v<int&>应该给出与 std::is_constructible_v<int&, const int&> 相同的结果做;然而,clang 7.0给出不同的结果,如上所示。

这种行为是否符合 C++ 标准?

最佳答案

is_copy_constructible的引用是什么?状态是:

If T is not a referenceable type (i.e., possibly cv-qualified void or a function type with a cv-qualifier-seq or a ref-qualifier), provides a member constant value equal to false. Otherwise, provides a member constant value equal to std::is_constructible<T, const T&>::value.

所以,这里 is_copy_constructible<T>::valuestd::is_constructible<T, const T&>::value相同.

所以在你的情况下:

std::is_constructible<int, const int&>::value将与 std::is_copy_constructible_v<int> 相同.

参见 DEMO

关于c++ - 为什么 std::is_copy_constructible 的行为不如预期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54474692/

相关文章:

c# - 将 C# DateTime 转换为 C++ std::chrono::system_clock::time_point

c++ - 从 char 数组获取 int32_t 或 int64_t 值

c++ - 函数返回类型中的 g++ auto 和 typename 关键字

c++ - 如何使用 Boost d_ary_heap?

namespaces - 使用 : http://docs. oasis-open.org/ws-sx/ws-trust/200512 或 http ://schemas. xmlsoap.org/ws/2005/02/trust 中的哪一个?

c++ - makefile:如何生成目标文件到上层目录

c++ - 特征矩阵行加法

c++ - 如何让 lambda 成为类(class)的 friend ?

c - C中变量修饰符的简单查询

c++ - 是否可以在 C++ 中重载运算符 "..."?