2017-08-14 89 views
2

我正在观察一些我无法完全解释自己的奇怪行为。 的代码看起来是这样的:当使用std :: addressof时出现GCC 7编译错误

#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失败。 Cla也不喜欢它。

(编辑)错误从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 

不过,我不能完全弄清楚为什么它不工作。

在此先感谢任何人试图帮助:)

+1

Please be mo具体比“失败”和“不喜欢它”。包含逐字错误消息。 – molbdnilo

+1

'addressof'需要一个变量名称,而不是类型名称 – meowgoesthedog

+0

对不起,我认为在godbolt中查看它更容易,但我想你说得对,在这里也有意义,所以我添加了它们;) | 和@meowgoesthedog不知道你的意思,变量通过变换传递给函数,只需要指定模板参数? – Christoph

回答

5

为了避免意外采取的临时地址,该库已经得到了第二(删除)签名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

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

+0

哦,哇,我以某种方式假定这些删除的重载将不会参与重载决议...非常感谢答案!这意味着我没有别的选择,只能用lambda包装这个调用,对吧? – Christoph

+1

@Christoph删除的重载专门用于参与重载解析,并为每个将解析给它们的函数调用给出编译时错误。否则,他们将完全无用。 –

+0

@Revolver_Ocelot感谢您的解释:)这对我来说完全有意义,我想我从来没有有意识地建立过这种连接:P – Christoph