libc ++is_copy_constructible对我来说似乎是错误的
libc++ is_copy_constructible seems wrong to me
is_copy_constructible的libc++实现是这样的:
template <class _Tp>
struct _LIBCPP_TYPE_VIS_ONLY is_copy_constructible
: public is_constructible<_Tp, const typename add_lvalue_reference<_Tp>::type>
{};
is_copy_constructible的C++规范很简单:
std::is_copy_constructible specification: std::is_constructible<T, const T&>::value is true.
但是,上面的实现不是实现 T&const 而不是 const T&吗?将 const 应用于add_lvalue_reference应该没有效果,并且至少有一个编译器 (EDG( 以警告的形式识别这一点。
演示问题的示例程序:
#include <type_traits>
struct ProofTest
{
ProofTest(){}
ProofTest(const ProofTest&) = delete; // is_copy_constructible should use this.
ProofTest(ProofTest&){ } // But instead it's using this.
};
void Proof()
{
static_assert(std::is_copy_constructible<ProofTest>::value == false, "is_copy_constructible bug");
}
在libstdc++下,上面的代码编译正常,但在libc++下,static_assert触发。
以下修复是否正确?
template <class _Tp>
struct _LIBCPP_TYPE_VIS_ONLY is_copy_constructible
: public is_constructible<_Tp, typename add_lvalue_reference<typename std::add_const<_Tp>::type>::type>
{};
这也影响了其他几个libc++类型的特征。
同意,感谢您的错误报告。
更新
相关问题:以下各项的预期值是多少:
std::is_constructible<int&>::value
?不太清楚 我从阅读标准。
标准说了什么:
对于可引用的类型
T
,结果与is_constructible<T, const T&>::value
相同,否则false
。
"可引用类型"基本上不是void
。 我在解释。 这不是一个确切的定义。 它的意思是可以理解的,而不是精确的。 语言律师(包括我自己(可以把它拆开。 但为了便于理解,"除了void
之外的任何东西"已经足够接近了。
所以你的问题变成了,什么是:
std::is_constructible<int&, const (int&)&>::value // I've used pseudo code
应用于引用const
是无操作 ((。 应用于左值引用的左值引用是无操作的(由于引用折叠(。 例如,考虑以下非便携式type_name
设施:
#include <type_traits>
#include <memory>
#include <iostream>
#include <cxxabi.h>
#include <cstdlib>
template <typename T>
std::string
type_name()
{
typedef typename std::remove_reference<T>::type TR;
std::unique_ptr<char, void(*)(void*)> own
(
abi::__cxa_demangle(typeid(TR).name(), nullptr,
nullptr, nullptr),
std::free
);
std::string r = own != nullptr ? own.get() : typeid(TR).name();
if (std::is_const<TR>::value)
r += " const";
if (std::is_volatile<TR>::value)
r += " volatile";
if (std::is_lvalue_reference<T>::value)
r += "&";
else if (std::is_rvalue_reference<T>::value)
r += "&&";
return r;
}
int
main()
{
typedef int& T;
std::cout << type_name<const T&>() << 'n';
}
对我来说,这打印出来了:
int&
因此,上述内容简化为:
std::is_constructible<int&, int&>::value // true
这应该回答true
因为左值int
应该可以从非常量左值int
构造。
相关文章:
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 我的固定时间步长与增量时间和插值的解决方案是错误的吗?
- 工会成员的析构函数似乎是自动调用的
- 为什么在尝试测量函数返回所需的时间时,我的运行时编号是错误的?
- 以相反的顺序打印数组 - 为什么这在C++中是错误的?
- C 程序如果功能中的语句不执行.在逻辑上似乎是错误的
- 对于特定情况的整数溢出似乎是由整数溢出引起的错误
- 尽管整个程序逻辑似乎在起作用,但答案是错误的
- Travis CI似乎是从错误的Stdlib读书
- 我的功能似乎是正确的,但我会收到编译错误
- 这似乎是C Primer第5版中的错误
- 这似乎是 clang++ 编译器中的一个错误
- 我正在收到错误的“ list :: ShowList”:功能风格的初始化器似乎是我的CPP文件中的函数定义'
- libc ++is_copy_constructible对我来说似乎是错误的
- 程序超过了时间限制,这似乎是不可能的——错误代码
- 错误C2448:函数式初始化器似乎是函数定义
- 模板演绎似乎是错误的
- visual C++,错误C2448-函数样式初始值设定项似乎是函数定义
- std::regex_match的结果似乎是错误的