Function pointer as template argument?

Is it possible to pass a pointer to a function as an argument to a template without using typedef?

template<class PF>
class STC {
    PF old;
    PF& ptr;
public:
    STC(PF pf, PF& p)
        : old(*p), ptr(p) 
    {
        p = pf;
    }
    ~STC() {
        ptr = old;
    }
};

void foo() {}
void foo2() {}

int main() {
    void (*fp)() = foo;
    typedef void (*vfpv)();
    STC<vfpv> s(foo2, fp); // possible to write this line without using the typedef?
}
+3
source share
3 answers

Yes:

STC< void (*)() > s(foo2, fp); // like this

This is the same as declaring typedefand deleting a keyword typedefand name.

+11
source

It is quite possible. I also recommend looking for boost :: function and boost :: bind as an alternative solution.

+3
source

boost, (, ), .

0

Source: https://habr.com/ru/post/1713814/


All Articles