Reputation: 8860
I have a following template:
template<typename Signature>
class TypeErasedFunctor;
template<typename R, typename... Args>
class TypeErasedFunctor<R(Args...)>
{
public:
virtual R operator()(Args... args) = 0;
};
It can be used to generate interface classes for functors like this:
using SemaphoreFunctor = TypeErasedFunctor<int(Semaphore&)>;
As can be seen the member function operator()
is non-const. I'd like to ask the following question - are there any options to select whether const or non-const variant should be created other than these two:
std::enable_if<>
) and additional argument for template,ConstTypeErasedFunctor
Upvotes: 0
Views: 236
Reputation: 599
You can have both (const and no const) versions of operator () on the same object as overloads. But if you want to easily select only one when declaring the object, you can use something like that:
template<bool b, typename Signature>
class TypeErasedFunctor;
template<typename R, typename... Args>
class TypeErasedFunctor<true,R(Args...)>
{
public:
virtual R operator() (Args... args) const = 0;
};
template<typename R, typename... Args>
class TypeErasedFunctor<false,R(Args...)>
{
public:
virtual R operator()(Args... args) = 0;
};
template<bool b>
using SemaphoreFunctor = TypeErasedFunctor<b,int(Semaphore&)>;
Later, in client code, you can choose with the remaining generic argument:
SemaphoreFunctor<true> object_with_const_op;
SemaphoreFunctor<false> object_with_no_const_op;
Upvotes: 3