2017-08-14 210 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