get <string>表示变体在clang ++下失败但不是g ++

时间:2017-09-30 19:35:50

标签: c++ c++17 clang++ variant

以下代码:

variant<string> x = "abc";
cout << get<string>(x) << "\n";

在g ++(版本7.2)下正常工作。但是,当使用libstdc ++在clang ++(版本5.0)下编译时,我在get方法中得到以下错误:

/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.2.0/../../../../include/c++/7.2.0/variant:238:46: fatal error: cannot cast 'std::variant<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >' to its private base class 'std::__detail::__variant::_Variant_storage<false, std::
__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >'
      return __get(std::in_place_index<_Np>, std::forward<_Variant>(__v)._M_u);

这是编译器错误,还是我的代码以任何方式违法?

1 个答案:

答案 0 :(得分:21)

这是由clang bug 31852(以及33222)引起的,其再现由Jonathan Wakely提供,看起来非常相关:

variant

clang没有正确识别具有占位符类型的朋友声明。这正是libstdc ++如何实现std::get

template<size_t _Np, typename _Vp>
friend constexpr decltype(auto) __detail::__variant::__get(_Vp&& __v);

这会访问__get的私有成员,但此功能为properly declared a friend

friend

libstdc ++的实现是有效的,clang并不认为<div qv-extension style="height: 100%; position: relative; overflow: auto;" class="ng-scope"> {{ html }} <div class="mydiv"> </div> </div> define( ["qlik", "jquery", "text!./style.css", "text!./template.html", "./d3/d3", ], function (qlik, $, cssContent, template ) {'use strict'; $("<style>").html(cssContent).appendTo("head"); return { template: template, initialProperties : { qHyperCubeDef : { qDimensions : [], qMeasures : [], qInitialDataFetch : [{ qWidth : 10, qHeight : 500 }] } }, definition : { type : "items", component : "accordion", items : { dimensions : { uses : "dimensions", min : 0 }, measures : { uses : "measures", min : 0 }, sorting : { uses : "sorting" }, settings : { uses : "settings", items : { initFetchRows : { ref : "qHyperCubeDef.qInitialDataFetch.0.qHeight", label : "Initial fetch rows", type : "number", defaultValue : 500 } } } } }, support : { snapshot: true, export: true, exportData : true }, paint: function ( ) { console.log("painting..."); var svg = d3.select(".mydiv") .append("svg") .attr("width", width) .attr("height", height); }, controller: ['$scope', function (/*$scope*/) { }] }; } );