[英]Type trait on std::reference_wrapper<std::any>
std::reference_wrapper
should be copy-constructible, regardless of the contained type (it's just a pointer inside). std::reference_wrapper
应该是可复制构造的,无论所包含的类型是什么(它只是内部的指针)。 However, when asserting that using 但是,当断言使用
static_assert(std::is_copy_constructible<std::reference_wrapper<std::any> >::value, "!");
Clang terminates with a compile error from inside the STL, while GCC reports no problems. Clang从STL内部以编译错误终止,而GCC没有报告任何问题。 This is not the case with other types (I tested
std::reference_wrapper<int>
and std::any
directly). 其他类型则不是这种情况(我直接测试了
std::reference_wrapper<int>
和std::any
)。
I have an example showing compilation in GCC and Clang at https://godbolt.org/z/7YwkWb . 我有一个示例,在https://godbolt.org/z/7YwkWb中显示了GCC和Clang中的编译。
As far as I understand it, even if the type given to std::is_copy_constructible
is not copy-constructible, it should just give a compile-time false, rather than failing compilation. 据我了解,即使给
std::is_copy_constructible
的类型不是可复制构造的,它也应该给出一个编译时错误,而不是编译失败。 What is the problem here? 这里有什么问题? Is that a compiler bug or am I missing something?
那是编译器错误还是我缺少什么?
The compile error from Clang is: Clang的编译错误是:
In file included from <source>:1:
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:132:31: error: no member named 'value' in 'std::is_copy_constructible<std::reference_wrapper<std::any> >'
: public conditional<_B1::value, _B2, _B1>::type
~~~~~^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/any:170:17: note: in instantiation of template class 'std::__and_<std::is_copy_constructible<std::reference_wrapper<std::any> >, std::is_constructible<std::reference_wrapper<std::any>, const std::reference_wrapper<std::any> &> >' requested here
enable_if<__and_<is_copy_constructible<_Tp>,
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/any:175:5: note: in instantiation of template type alias '__any_constructible' requested here
using __any_constructible_t =
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/any:181:56: note: in instantiation of template type alias '__any_constructible_t' requested here
__any_constructible_t<_Tp, _ValueType&&> = true,
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/any:183:7: note: while substituting prior template arguments into non-type template parameter [with _ValueType = const std::reference_wrapper<std::any> &, _Tp = std::reference_wrapper<std::any>, _Mgr = std::any::_Manager_internal<std::reference_wrapper<std::any> >]
any(_ValueType&& __value)
^~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:921:56: note: while substituting deduced template arguments into function template 'any' [with _ValueType = const std::reference_wrapper<std::any> &, _Tp = (no value), _Mgr = (no value), $3 = (no value), $4 = (no value)]
: public __bool_constant<__is_constructible(_Tp, _Args...)>
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:933:14: note: in instantiation of template class 'std::is_constructible<std::reference_wrapper<std::any>, const std::reference_wrapper<std::any> &>' requested here
: public is_constructible<_Tp, const _Tp&>
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:939:14: note: in instantiation of template class 'std::__is_copy_constructible_impl<std::reference_wrapper<std::any>, true>' requested here
: public __is_copy_constructible_impl<_Tp>
^
<source>:12:20: note: in instantiation of template class 'std::is_copy_constructible<std::reference_wrapper<std::any> >' requested here
static_assert(std::is_copy_constructible<std::reference_wrapper<std::any> >::value, "!");
^
In file included from <source>:1:
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:137:31: error: no member named 'value' in 'std::is_copy_constructible<std::reference_wrapper<std::any> >'
: public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type
~~~~~^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/any:192:27: note: in instantiation of template class 'std::__and_<std::is_copy_constructible<std::reference_wrapper<std::any> >, std::__not_<std::is_constructible<std::reference_wrapper<std::any>, const std::reference_wrapper<std::any> &> >, std::__not_<std::__is_in_place_type<std::reference_wrapper<std::any> > > >' requested here
enable_if_t<__and_<is_copy_constructible<_Tp>,
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/any:196:7: note: while substituting prior template arguments into non-type template parameter [with _ValueType = const std::reference_wrapper<std::any> &, _Tp = std::reference_wrapper<std::any>, _Mgr = std::any::_Manager_internal<std::reference_wrapper<std::any> >]
any(_ValueType&& __value)
^~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:921:56: note: while substituting deduced template arguments into function template 'any' [with _ValueType = const std::reference_wrapper<std::any> &, _Tp = (no value), _Mgr = (no value), $3 = (no value)]
: public __bool_constant<__is_constructible(_Tp, _Args...)>
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:933:14: note: in instantiation of template class 'std::is_constructible<std::reference_wrapper<std::any>, const std::reference_wrapper<std::any> &>' requested here
: public is_constructible<_Tp, const _Tp&>
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:939:14: note: in instantiation of template class 'std::__is_copy_constructible_impl<std::reference_wrapper<std::any>, true>' requested here
: public __is_copy_constructible_impl<_Tp>
^
<source>:12:20: note: in instantiation of template class 'std::is_copy_constructible<std::reference_wrapper<std::any> >' requested here
static_assert(std::is_copy_constructible<std::reference_wrapper<std::any> >::value, "!");
^
2 errors generated.
Compiler returned: 1
The error you see clearly states at the top that std::is_copy_constructible
doesn't have a value
member for the specialization you check. 您看到的错误在顶部清楚地表明
std::is_copy_constructible
没有针对您检查的专业化的value
成员。 Now, the types you pass don't violate the contract of std::is_copy_constructible
, so there is no risk of undefined behavior. 现在,您传递的类型不会违反
std::is_copy_constructible
,因此不存在未定义行为的风险。
Which means only one thing, it's not a compiler issue, but a standard library implementation issue. 这仅意味着一件事,这不是编译器问题,而是标准库实现问题。 Clang uses the default installed standard library implementation, which on the system
godbolt
is running is libstdc++
(the GNU implementation). Clang使用默认安装的标准库实现,在系统
godbolt
上运行的是libstdc++
(GNU实现)。 This evidently causes the issue due to some possible soft incompatibility. 这显然是由于某些可能的软件不兼容而导致的问题。
If you specify the -stdlib=libc++
option ( libc++
is the LLVM implementation of the standard library) when building with Clang, then it accepts the code fine. 如果在使用Clang进行构建时指定了
-stdlib=libc++
选项( libc++
是标准库的LLVM实现),则它将接受代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.