【发布时间】:2013-11-19 01:50:04
【问题描述】:
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++ 类型特征。
【问题讨论】:
标签: c++ c++11 typetraits libc++