Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::array as a parameter in virtual function

Tags:

c++

arrays

c++11

I want to send std::array as a parameter to my virtual function

class Handler:
    {
    public:
        template <std::size_t N>
        virtual void handle(const std::array<char, N>& msg, std::vector<boost::asio::const_buffer>& buffers) = 0;
    };

but gcc said templates may not be 'virtual'. So how i pass a std::array to my function?

like image 958
Mike Minaev Avatar asked Mar 16 '23 06:03

Mike Minaev


1 Answers

A member function template may not be virtual. See this question.

However, you can make a virtual member function takes a std::array of specific size by moving the N out to the Handler:

template <size_t N>
class Handler:
{
public:
    virtual void handle(const std::array<char, N>& msg,
        std::vector<boost::asio::const_buffer>& buffers) = 0;
};

Or simply passing a vector<char>, which given the context, could make more sense:

class Handler:
{
public:
    virtual void handle(const std::vector<char>& msg,
        std::vector<boost::asio::const_buffer>& buffers) = 0;
};
like image 90
Barry Avatar answered Mar 24 '23 13:03

Barry