std :: visit的下列用法在gcc 7.2下正確編譯,但無法在鏗鏘5.0下編譯。有誰知道問題是什麼?std :: visit for variant無法在clang下編譯5
#include <variant>
struct S1 {int foo() { return 0; }};
struct S2 {int foo() { return 1; }};
using V = std::variant<S1, S2>;
int bar() {
V v;
return std::visit([](auto& s) { return s.foo(); }, v);
}
的第一個錯誤是:
include/c++/7.2.0/variant:238:46: error: cannot cast 'std::variant<S1, S2>' to its private base class
'std::__detail::__variant::_Variant_storage<true, S1, S2>'
return __get(std::in_place_index<_Np>, std::forward<_Variant>(__v)._M_u);
這裏是godbolt一個鏈接顯示此錯誤:https://godbolt.org/g/5iaKUm
libstdC++的變種可能會使用Clang不支持的非標準功能。它用'-stdlib = libC++'編譯。 – chris
注意'std :: get <0>(v)','std :: get(v)'等等會失敗,並帶有基本相同的錯誤(以及更少的實例化步驟)。 –
aschepler
也許是一個騙局,取決於你如何考慮欺騙:https://stackoverflow.com/q/46506387/2069064 – Barry