【发布时间】:2019-10-15 11:22:36
【问题描述】:
我正在更新一个项目以使用 C++17,并发现一些遵循此模式的代码在最新版本的 clang 上导致编译错误的情况:
#include <boost/variant.hpp>
struct vis : public boost::static_visitor<void>
{
void operator()(int) const { }
};
int main()
{
boost::variant<int> v = 0;
boost::apply_visitor(vis{}, v);
}
Using clang v8.0 in C++17 mode, this fails with the following error:
<source>:11:30: error: temporary of type 'boost::static_visitor<void>' has protected destructor
boost::apply_visitor(vis{}, v);
^
/opt/compiler-explorer/libs/boost_1_64_0/boost/variant/static_visitor.hpp:53:5: note: declared protected here
~static_visitor() = default;
但是,it compiles cleanly in C++14 mode。我发现如果我将大括号初始化vis{} 更改为括号vis(),那么它在两种模式下都能正确编译。我尝试过的每个 gcc 版本都允许在 C++17 模式下使用这两种变体。
这是从 C++14 到 C++17 的正确行为变化,还是一个 clang 错误?如果它是正确的,为什么它现在在 C++17 中无效(或者它可能一直是,但 clang 只是在早期的标准版本中允许它)?
【问题讨论】:
标签: c++ clang c++17 boost-variant