mardi 5 décembre 2023

std::function - function uses ‘auto’ type specifier without trailing return type in C++ 11 [duplicate]

I'm trying to get a C++14 library working with our C++ 11 code base and having trouble with using std::function (of which I know very little).

Minimal code:

#include <functional>
#include <iostream>

class A
{
public:
    A() {}
    ~A() {}
    
    using CallbackFn = std::function<int(int, double)>;
    auto GetCallbackFn() const { return mCallbackFcn.target<int (*)(int, double)>(); }

private:
   CallbackFn mCallbackFcn;

};

int main()
{
    std::cout << "hi there\n";
}

The code compiles without error with C++14 but with C++11 produces an error:

main.cpp:20:5: error: ‘GetCallbackFn’ function uses ‘auto’ type specifier without trailing return type

I've tried replacing auto with CallbackFn and other permutations but all end up with one error or another.

How do I modify the code to compile under C++11?

Thanks

Aucun commentaire:

Enregistrer un commentaire