vendredi 28 avril 2017

Select a constructor based on template parameters

I would like to alter a classes functions based on the value of a template parameter. Using this post, all of the member functions can be written differently based on the template parameter and that works great. The problem is that example is using the return type of the function so it doesn't work with constructors. Based on this post and this post, I have tried:

// I'm using gcc 4.8.4 so have to define enable_if_t myself although 
// I guess it's not really needed

template< bool B, class T = void >
using enable_if_t = typename std::enable_if<B,T>::type;

template<class T, std::size_t CAPACITY, bool USE_THREADS = true>
class C_FIFO
{
public:
//...other ctor defs

    template<bool trigger = USE_THREADS, enable_if_t<not trigger>* = nullptr >
    C_FIFO(const C_FIFO& that):
        m_buf_capacity(CAPACITY + 1), m_in_ctr(0), m_out_ctr(0), m_wait(true)
    {
        m_buffer_data = that.m_buffer_data;
        m_in_ctr = that.m_in_ctr;
        m_out_ctr = that.m_out_ctr;
        m_wait    = that.m_wait.load();
    }
// more stuff
}

and

template<class T, std::size_t CAPACITY, bool USE_THREADS = true>
class C_FIFO
{
public:
//...other ctor defs

    template<bool trigger = USE_THREADS>
    //enable_if_t<not trigger>
    C_FIFO(const C_FIFO& that, enable_if_t<not trigger, bool> t = false):
        m_buf_capacity(CAPACITY + 1), m_in_ctr(0), m_out_ctr(0), m_wait(true)
    {
        m_buffer_data = that.m_buffer_data;
        m_in_ctr = that.m_in_ctr;
        m_out_ctr = that.m_out_ctr;
        m_wait    = that.m_wait.load();
    }
// other stuff
}

but it both cases the compiler tries to use the default copy constructor which is deleted because the class contains non-copyable types (mutex and condition variable). Both cases seem like they should work, but apparently I'm not seeing something :). Any pointers (no pun intended) would be appreciated.

Aucun commentaire:

Enregistrer un commentaire