c++boost-threadcodewarriormetrowerks

Solve boost.thread compilation error with Metrowerks compiler


I'm trying to use boost.thread with metrowerks codewarrior 5.5.3; in the header thread.hpp, I get the error that he's redefining thread::thread_data:

class BOOST_THREAD_DECL thread
{
private:
    ...        
    template<typename F>
    struct thread_data:
        detail::thread_data_base
    {
        F f;

        thread_data(F f_):
            f(f_)
        {}
        thread_data(detail::thread_move_t<F> f_):
            f(f_)
        {}

        void run()
        {
            f();
        }
    };
    ...
 };

template<typename F>
struct thread::thread_data<boost::reference_wrapper<F> >:
    detail::thread_data_base
{
    F& f;

    thread_data(boost::reference_wrapper<F> f_):
        f(f_)
    {}

    void run()
    {
        f();
    }
};

I see that, in effect, thread::thread_data seems to be declared twice. What C++ feature is used there? How can I overcome that compiler deficiency?


Solution

  • The second instance is a partial specialization of the template class, this is valid C++ and should not result in a redefinition error.

    I've had problems with such features in a metrowerks compilers in the past too though, more specifically, when using template template parameters with default values, the compiler would never compile it. My workaround was rather easy, don't provide a default value... (1)

    If I were you I'd try adding a full specialization for your specific type, and hope the compiler uses some different compile path for those and gets you past this.... (this is just a wild guess, I don't have/use a metrowerks compiler these days)

    typedef boost::function< void () > MyThreadFunction; // or whatever you need
    
    template <>
    struct thread::thread_data<boost::reference_wrapper< MyThreadFunction > >:
        detail::thread_data_base
    {
        ....
    };
    

    (1) To be honest, this was many years ago, I don't think any compiler compiled templates fully back then.