簡體   English   中英

隱式函數到函數指針轉換不適用於標准容器(例如向量)

[英]Implicit function-to-function-pointer conversion not working for std containers (e.g. vector)

我最近了解到 C++ 可以在必要時執行隱式函數到函數指針轉換 例如,在下面的示例中, my_func_1my_func_2是等價的。 但是vector_1vector_2不是,實際上vector_2會報編譯錯誤。 其他標准容器也會發生類似的編譯錯誤,例如unordered_map 為什么會這樣?

class my_class {
    // dummy class
};

int main() {
    vector<my_class(*)()> vector_1; // correct one
    vector<my_class()> vector_2;    // gives compile error
    
    // However, both of the below are OK due to implicit
    // function-to-function-pointer conversion
    void my_func_1(my_class(*)());
    void my_func_2(my_class());    // equivalent to the one above
}

這是編譯錯誤消息(縮短以突出顯示錯誤):

/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/ext/new_allocator.h:96:7:錯誤: 'address' 的多個重載實例化為相同的簽名 '__gnu_cxx::new_allocator<my_class ()>::const_pointer (__gnu_cxx::new_allocator<my_class ()>::const_reference) const noexcept' (又名 'my_class (*(my_class ( &)()) const noexcept)()') 地址(const_reference __x) const _GLIBCXX_NOEXCEPT ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../。 ./include/c++/9/bits/allocator.h:112:30: 注意:在模板 class '__gnu_cxx::new_allocator<my_class ()>' 的實例化中 class< allocator: public __gnu_cxx::new_allocator<my_class ()>' bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/ext/alloc_traits.h:49:47:注意:在實例化模板 class 'std::allocator<my_class ()>' 在這里請求模板<typename _Alloc, typename = typename _Alloc::value_type> ^ /usr/bin/../lib/gcc/x86_64-linux-g nu/9/../../../../include/c++/9/bits/stl_vector.h:83:35: 注意:在 '__alloc_traits<std::allocator<my_class ( )>>' 這里需要 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template ^~~~~~~~~~~~~~~~~~~~~~ /usr/bin/../lib/ gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:386:30:注意:在模板 class 'std::_Vector_base 的實例化中<my_class (), std::allocator<my_class ()>>' 在此處請求 class 向量:受保護 _Vector_base<_Tp, _Alloc> ^ 第 7 行:字符 24:注意:在模板 class 'std::vector<my ), std::allocator<my_class ()>>' 在這里請求 vector<my_class()> vector_2; // 給出編譯錯誤 ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/ext/new_allocator.h: 92:7:注意:之前的聲明在這里地址(參考 __x)const _GLIBCXX_NOEXCEPT ^

在此聲明中:

vector<my_class()> vector_2; 

用於實例化向量的類型是myclass() ,它是 function 類型。 具體來說,它是一個 function ,它不接受 arguments,並返回一個my_class

一個 function 類型是不可復制分配的,從這個測試可以看出:

static_assert(not std::is_copy_assignable_v<Foo()>);

可復制分配是對用作std::vector模板參數的類型的要求之一,因此此聲明無法編譯。


其余的聲明很好:

vector<my_class(*)()> vector_1; // vector of function pointer type

由於隱式轉換為 function 參數中使用的類型的函數指針,以下聲明是等效的:

void my_func_1(my_class(*)());  // function taking a function pointer 
                                // to a function that takes no arguments
                                // and returns a my_class

void my_func_2(my_class());     // function taking a function that takes no 
                                // arguments and returns a my_class 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM