mercredi 3 février 2016

Functions and function pointers in C++

With reference to the following code

#include <iostream>
using std::cout;
using std::endl;
#include <vector>
using std::vector;

void function() {
    cout << "Hello World" << endl;
}

int main() {
    vector<void (*) ()> functions;
    functions.push_back(function);         // (1) no error
    functions.push_back(&function);        // (2) no error
    for (const auto& func : functions) {
        func();
    }

    // vector<decltype(function)> vec;     // (3) error
    return 0;
}

There seems to be an error when I uncomment (3), I am just trying to understand the reasoning behind this. When I pass in a function as the argument to a templated function does it resolve the type to a function pointer? It would make sense for the compiler to deduce all function types as such to a function pointer but why then does the decltype() not resolve to a function pointer?

Aucun commentaire:

Enregistrer un commentaire