C ++ 0x auto cannot output type of vector pointer of function <int>

Using GCC 4.7.0 (g ++ -std = C ++ 0x test.cpp) to compile the following simple C ++ code gives a compilation error message: error: 'auto from' & std :: vector <_Tp, _Alloc cannot be output > :: push_back>

I ask why, in this simple case, auto cannot infer the type of a pointer to a member function?

#include <iostream>
#include <vector>

int main(void) {

    // works 
    void (vector<int>::*pb)(const vector<int>::value_type&)
        = &vector<int>::push_back;

    // not work
    auto pbb = std::mem_fn(&vector<int>::push_back);

    return 0;
}
+4
source share
2 answers

++ 11 โ€‹โ€‹ vector::push_back, rvalue. mem_fn , , .

auto pbb = std::mem_fn(static_cast<void (vector<int>::*)(const vector<int>::value_type&)>(&vector<int>::push_back));

, , , - . , .., .

ยง17.6.5.5/2 [member.functions]

-, ++, --, , -, , , . [. , - - -. -end note]

+9

, :

std::mem_fn<void(const int&)>(&vector<int>::push_back);

std::mem_fn<void(int&&)>(&vector<int>::push_back);
+2

Source: https://habr.com/ru/post/1629659/


All Articles