[英]Cannot use a constant reference of experimental::optional when using clang & libstdc++
我正在不断引用一个实验性的::: optional <>变量,但是当我在其上使用operator->()时,会出现编译错误,但仅当使用clang ++ 和 libstdc ++时才如此。
#include <experimental/optional>
#include <iostream>
#include <vector>
int main(void)
{
std::experimental::optional<std::vector<int>> opt;
const auto &rf = opt;
opt.emplace();
opt->push_back(1);
std::cout << "opt->size() = " << opt->size()
<< " rf->size() = " << rf->size() << "\n";
return 0;
}
运行该程序:
$ clang++ -W -Wall -std=c++14 -stdlib=libc++ test.cc && ./a.out
opt->size() = 1 rf->size() = 1 # OK
$ g++ -W -Wall -std=c++14 test.cc && ./a.out
opt->size() = 1 rf->size() = 1 # OK
$ clang++ -W -Wall -std=c++14 test.cc
In file included from test.cc:1:
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/optional:576:16: error: call to '__constexpr_addressof' is ambiguous
{ return __constexpr_addressof(this->_M_get()); }
^~~~~~~~~~~~~~~~~~~~~
test.cc:14:40: note: in instantiation of member function 'std::experimental::fundamentals_v1::optional<std::vector<int, std::allocator<int> > >::operator->' requested here
<< " rf->size() = " << rf->size() << "\n";
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/optional:173:20: note: candidate function [with _Tp = const std::vector<int, std::allocator<int> >, $1 = <>]
constexpr _Tp* __constexpr_addressof(_Tp& __t)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/optional:180:17: note: candidate function [with _Tp = const std::vector<int, std::allocator<int> >, $1 = <>]
inline _Tp* __constexpr_addressof(_Tp& __t)
^
1 error generated.
每当我使用常量引用时,都会发生此错误:显然,非常量引用是可以的。
我将Linux Mint 18与以下版本的g ++ / libstdc ++ 5.4.0和clang ++ 3.8.0结合使用:
$ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ dpkg-query -s libstdc++-5-dev
...
Version: 5.4.0-6ubuntu1~16.04.4
...
$ clang++ --version
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
这是我的程序,libstdc ++或clang ++中的错误吗?
更新 :clang 3.9.0解决了问题。 (如下面由Richard Smith和Ville Voutilainen解释的那样,似乎libstdc ++期望编译器以某种与clang 3.8.0不匹配的方式运行。)
在我看来,这肯定像是个Clang错误,因为它诊断为模棱两可的重载具有互斥的enable_if约束,因此它们不可能模棱两可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.