Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally provide a using declaration

Suppose I've got a class foo with template parameter T and I want to provide a using declaration for the reference and const-reference types corresponding to T:

template<typename T>
struct foo
{
    using reference = T&;
    using const_reference = T const&;
};

Is there a way to "enable" these using declerations only if T is not void without speclializing the whole class foo?

like image 895
0xbadf00d Avatar asked May 06 '26 04:05

0xbadf00d


1 Answers

You could inherit from a base class with a specialization for void:

template<typename T>
struct typedefs {
    using reference = T&;
    using const_reference = T const&;
};

template<>
struct typedefs<void> {};

template<typename T>
struct foo : typedefs<T>
{};
like image 122
Ted Lyngmo Avatar answered May 09 '26 21:05

Ted Lyngmo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!