Trying to derive a class from std::function
, and for starters inherit the constructors. This is what I guessed at:
#include <iostream>
#include <functional>
using namespace std;
template< class R, class... Args >
class MyFunction : public std::function<R(Args...)> {
public:
using std::function<R(Args...)>::function;
};
int main() {
std::function<void()> f_display_42 = []() { std::cout << 42; }; //works
MyFunction<void()> mine = []() { std::cout << 42; }; //errors
}
Errors that come back are:
prog.cpp: In instantiation of ‘class MyFunction<void()>’:
prog.cpp:14:24: required from here
prog.cpp:6:7: error: function returning a function
class MyFunction : public std::function<R(Args...)> {
^
prog.cpp:8:38: error: function returning a function
using std::function<R(Args...)>::function;
^
prog.cpp:8:38: error: using-declaration for non-member at class scope
prog.cpp: In function ‘int main()’:
prog.cpp:14:55: error: conversion from ‘main()::__lambda1’
to non-scalar type ‘MyFunction<void()>’ requested
MyFunction<void()> mine = []() { std::cout << 42; };
In GCC 4.8.2.
To add to chris' answer, if you want your current definition to work, use
MyFunction<void> mine = []() { std::cout << 42; };
If you want the same nifty MyFunction<R(Args...)>
syntax as std::function
, then you must supply a primary class template that's unimplemented, and a specialization that derives from std::function
(this is exactly what std::function
does too).
template< class R, class... Args >
class MyFunction;
template< class R, class... Args >
class MyFunction<R(Args...)> : public std::function<R(Args...)>
{ ... }