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

标签 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;});
}

这是一个可以玩的版本 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

但是,我不太明白为什么它不起作用。

提前感谢任何试图提供帮助的人:)

最佳答案

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

template <class T> constexpr T* addressof(T& r) noexcept;
template <class T> const T* addressof(const T&& elem) = delete;

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

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

关于c++ - 使用 std::addressof 时出现 GCC 7 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45670453/

相关文章:

c - GCC 4.7 字符串文字的源字符编码和执行字符编码?

c++ - gcc 不警告 "variable set but not used"

c++ - uintptr_t 和 size_t 相同吗?

c++ - 没有匹配函数调用 'AnimationSet::AnimationSet()'

c++ - multimap 与next_permutation C++的结合使用

c - 有效地将 16 位 short 转换为 8 位 char

c++ - std::vector 在代码拆分后无法引用要插入的对象

c++ - std::function 可以从右值引用移动构造到临时仿函数对象吗?

c++ - g++ Cygwin/Linux 或版本差异

c++ - 在 Linux 下从 C++ 中的串行接口(interface)读取字节的问题