0

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.

  • Most types in `std` are not designed to be inherited from, including `std::function`. – Yakk - Adam Nevraumont Dec 03 '14 at 03:36
  • @Yakk Thanks for the reminder, though the idea of "should you ever" is a bit controversial with the likes of the ["Thou shalt not inherit from std::vector"](http://stackoverflow.com/questions/4353203/thou-shalt-not-inherit-from-stdvector) debate. This is just a local experiment for the moment, however. – HostileFork says dont trust SE Dec 03 '14 at 03:40
  • You might as well do it privately and just have using declarations for what you want to carry through. That leaves no chance for the lack of deriving support from the base class. – chris Dec 03 '14 at 05:25

2 Answers2

2
MyFunction<void()> mine

According to your class, this deduces R as void() and Args... as an empty parameter pack. If you want that syntax to work, you have to specialize your class template:

template<class R, class... Args>
class MyFunction<R(Args...)> : blah
chris
  • 60,560
  • 13
  • 143
  • 205
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...)>
{ ... }

Live demo

Community
  • 1
  • 1
Praetorian
  • 106,671
  • 19
  • 240
  • 328