acegs
acegs

Reputation: 2809

typedef on std::function to shorten it

I want to shorten the typing of std::function<bool(int)> to just func<bool(int)> in my own namespace. It's just a personal preference.

I tried the following code below but I'm encountering syntax errors.

//MyHeader.hpp
template<typename S> struct func; //signature

template<typename R, typename ...Args>
struct func<R(Args...)>;

template<typename R, typename ...Args>
//typename typedef std::function<R(Args...)> func; // <R(Args...)>;
using func = std::function<R(Args...)>;  //<---closes to solve.


//SomeClass.cpp
func<bool(int)> f;
func<void()> g = [] {
    //some code here...
}

How can I achieve this?

Upvotes: 2

Views: 251

Answers (1)

lubgr
lubgr

Reputation: 38287

Your solution is a bit too complicated, you might want to use the alias template like this

#include <functional>

template <class Fct> using func = std::function<Fct>;

which you can instantiate the in the desired way:

int test(bool) { return 1; }

func<int(bool)> f(test);
funct<void()> g = [](){};

Upvotes: 3

Related Questions