I have a template class that needs a callable object to be passed to handle errors.
The callable object must overload void operator()(const boost::system::error_code&)
, and its type is passed as a template parameter to make the class more generic (e.g. accept lambdas).
The class looks like this
template <typename AsyncStream,
typename ErrorHandler,
typename = std::enable_if_t<...>>
struct basic_api {
...
private:
AsyncStream sock_;
ErrorHandler error_handler_;
};
Now, I want to create an instance of this class in another class that overloads void operator()(const boost::system::error_code&)
Test fixture example:
struct BasicApiTest : ::testing::Test {
void operator()(const boost::system::error_code& err) {
// handler errors
}
std::shared_ptr<basic_api<tikpp::tests::fakes::socket, ???>> api;
};
I tried to solve this by creating an additional class which has the error handling function overload, and then derive from it, like so:
struct error_handler {
void operator()(const boost::system::error_code& err)
// handle errors
}
};
struct BasicApiTest : error_handler, ::testing::Test {
using socket_type = tikpp::tests::fakes::socket;
using handler_type = error_handler;
using api_type = tikpp::basic_api<socket_type, handler_type>;
BasicApiTest() : api{...} {}
std::shared_ptr<api_type> api;
};
But this solution has a lot of limitations.
Is this doable in C++, or should I find another approach to pass error handlers?