简体   繁体   中英

template argument as functor template

I'm trying to create a thread class that executes a functor from an adapter. The code shows my attempt.

#include <iostream>

struct null_t { };

typedef void (*thread_func_t)();
typedef void (*thread_func_2_t)(int);

template <typename F, typename P = null_t>
class adapter
{
public:
    adapter(F f, P p = null_t()) : _f(f), _p(p) {}

    void operator ()() 
    {
        _f(_p);
    }
private:
    F _f;
    P _p;
};

template <typename T>
class thread
{
public:
    explicit thread(T f) : _f(f) { }

    void run()
    {
        _f();
    }
private:
    T _f;
};

void show_hello()
{
    std::cout << "hello" << std::endl;
}

void show_num(int x)
{
    std::cout << "show_num: " << x << std::endl;
}

int main()
{
    thread<adapter<thread_func_t> > t_a(adapter<thread_func_t>(&show_hello));

    t_a.run();

    int i = 666;
    thread<adapter<thread_func_2_t, int> > t_b(adapter<thread_func_2_t, int>(&show_num, i));
    t_b.run();
}

The compiler error:

$ /usr/bin/g++-4.4 func.cpp -o func
func.cpp: In function ‘int main()’:
func.cpp:51: error: request for member ‘run’ in ‘t_a’, which is of non-class type ‘thread<adapter<void (*)(), null_t> >(adapter<void (*)(), null_t>&)’

1) The adapter is not prepared to call a function without parameter (I don't know how to do that).

2) I've tried thread receiving a template parameter without success.

I'm trying to do almost the same as the sample bellow (this Adapter does not work with a function that does not have parameter):

typedef void (*WorkerFunPtr)(const std::string&);

template<typename FunT, typename ParamT>
struct Adapter {
    Adapter(FunT f, ParamT& p) : f_(f), p_(&p) {}

    void operator( )( ) {
        f_(*p_);
    }
    private:
    FunT f_;
    ParamT* p_;
};

void worker(const std::string& s) { std::cout << s << '\n'; }

int main( ) {
    std::string s1 = "This is the first thread!";
    boost::thread thr1(Adapter<WorkerFunPtr, std::string>(worker, s1));

    thr2.join( );
}

This is the Most Vexing Parse problem. You need to add another pair of parentheses around the constructor argument, otherwise the line is treated as a function declaration.

thread<adapter<thread_func_t> > t_a((adapter<thread_func_t>(&show_hello)));

Also, consider using boost::thread , as it will turn your code into a three-liner.

Using the uniform initialization syntax "{}"

thread<adapter<thread_func_t> > t_a{adapter<thread_func_t>(&show_hello)};

avoids this problem.

For completeness, another way to solve this vexing problem is by making an adapter instance first and then passing that into your thread class during construction:

adapter<thread_func_t> some_adapter_delegate_thingy(&show_hello);
thread<adapter< thread_func_t> > t_a(some_adapter_delegate_thingy);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM