I have a template class that should allow void
as a template parameter. This class does have a function that passes a reference of the parameter, so I did the following:
template <typename T>
struct trait
{
typedef typename boost::conditional<
boost::is_void<T>::value,
void, T &
>::type type;
};
template <typename T>
struct foo
{
typename trait<T>::type ref()
{
// do something
}
};
Yet the compiler claims I would form a reference to void
in instantiation of struct trait<void>
. Why so and how can I achieve what I want?
Well, you clearly form a reference to void
in your conditional type definition when you say T&
. This seems to be best dealt with a specialization:
template <typename T> struct trait { typedef T& type; };
template <> struct trait<void> { typedef void type; };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With