I am writing a template class that stores std::functionso I can call it later. Here is the simplified code:
template <typename T>
struct Test
{
void call(T type)
{
function(type);
}
std::function<void(T)> function;
};
The problem is that this template does not compile for the type void, because
void call(void type)
becomes undefined.
Specialization for a type voiddoes not alleviate the problem because
template <>
void Test<void>::call(void)
{
function();
}
still incompatible with ad call(T Type).
So, using the new features of C ++ 11, I tried std::enable_if:
typename std::enable_if_t<std::is_void_v<T>, void> call()
{
function();
}
typename std::enable_if_t<!std::is_void_v<T>, void> call(T type)
{
function(type);
}
but it does not compile with Visual Studio:
error C2039: 'type': is not a member of 'std :: enable_if'
How would you deal with this problem?
source
share