这是我的基本测试代码:
#include <iostream>
#include <variant>
int main(int argc, char** argv) {
std::variant<int> v{5};
std::cout << std::get<0>(v) << std::endl;
}
当我使用Clang编译它时,如下所示:clang++ -std=c++1z -g Sources/main.cpp
我收到了这些错误:
In file included from Sources/main.cpp:2:
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/variant:235:46: error: cannot cast 'std::variant<int>' to its private base class
'std::__detail::__variant::_Variant_storage<true, int>'
return __get(std::in_place_index<_Np>, std::forward<_Variant>(__v)._M_u);
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/variant:1192:35: note: in instantiation of function template specialization
'std::__detail::__variant::__get<0, std::variant<int> &>' requested here
return __detail::__variant::__get<_Np>(__v);
^
Sources/main.cpp:6:20: note: in instantiation of function template specialization 'std::get<0, int>' requested here
std::cout << std::get<0>(v) << std::endl;
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/variant:878:7: note: constrained by private inheritance here
: private __detail::__variant::_Variant_base<_Types...>,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/variant:235:74: error: '_M_u' is a private member of
'std::__detail::__variant::_Variant_storage<true, int>'
return __get(std::in_place_index<_Np>, std::forward<_Variant>(__v)._M_u);
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/variant:878:7: note: constrained by private inheritance here
: private __detail::__variant::_Variant_base<_Types...>,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/variant:377:34: note: member is declared here
_Variadic_union<_Types...> _M_u;
但是当我用GCC编译时,使用相同的选项,我没有任何错误,我得到了正确的输出(5
)。
所以我想知道它是否真的是一个实现错误,或者它是否来自我。