使用std :: addressof时出现GCC 7编译错误

has*_*sec 3 c++ gcc std gcc7

我观察到一些我无法解释的奇怪行为.代码如下所示:

#include <memory>
#include <vector>
#include <algorithm>

int main(){
    std::vector<double> t1(10, 5.0);
    std::vector<double*> t2(10);
    std::transform(t1.begin(), t1.end(), t2.begin(), std::addressof<double>);
    //std::transform(t1.begin(), t1.end(), t2.begin(), [](double& a){return &a;});
}
Run Code Online (Sandbox Code Playgroud)

这里有一个版本来玩https://godbolt.org/g/YcNdbf 问题是这个代码使用gcc4.9-6.3编译好但在gcc 7.1下失败.Clang也不喜欢它.

(编辑)来自gcc 7.1的错误消息:

<source>: In function 'int main()':
8 : <source>:8:76: error: no matching function for call to 'transform(std::vector<double>::iterator, std::vector<double>::iterator, std::vector<double*>::iterator, <unresolved overloaded function type>)'
     std::transform(t1.begin(), t1.end(), t2.begin(), std::addressof<double>);
                                                                            ^
In file included from /opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/algorithm:62:0,
                 from <source>:3:
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/stl_algo.h:4281:5: note: candidate: template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)
     transform(_InputIterator __first, _InputIterator __last,
     ^~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/stl_algo.h:4281:5: note:   template argument deduction/substitution failed:
8 : <source>:8:76: note:   could not resolve address from overloaded function 'addressof<double>'
     std::transform(t1.begin(), t1.end(), t2.begin(), std::addressof<double>);
                                                                            ^
In file included from /opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/algorithm:62:0,
                 from <source>:3:
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/stl_algo.h:4318:5: note: candidate: template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)
     transform(_InputIterator1 __first1, _InputIterator1 __last1,
     ^~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/stl_algo.h:4318:5: note:   template argument deduction/substitution failed:
8 : <source>:8:76: note:   could not resolve address from overloaded function 'addressof<double>'
     std::transform(t1.begin(), t1.end(), t2.begin(), std::addressof<double>);
                                                                            ^
Compiler exited with result code 1
Run Code Online (Sandbox Code Playgroud)

但是,我无法弄清楚它为什么不起作用.

在此先感谢任何想要帮助的人:)

Bo *_*son 7

为避免意外获取临时地址,图书馆获得了第二个(已删除)签名addressof:

template <class T> constexpr T* addressof(T& r) noexcept;
template <class T> const T* addressof(const T&& elem) = delete;
Run Code Online (Sandbox Code Playgroud)

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2598

所以现在编译器不知道你的代码是否应该与删除的函数匹配......

  • @Christoph 也,除了在 lambda 中包装调用之外,您还可以通过转换为具有指定签名的函数的指针来具体说明要使用哪个重载:https://godbolt.org/g/UshjQS (2认同)