Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent template instantiation when template argument is zero

I have a templated class

template< std::size_t Size >
class Buffer
{
....
};

I'd like to prevent instantiation of this template when the Size argument is zero. i.e. generate a compiler warning for the following.

Buffer< 0 > buf;

but all other variants would work.

Buffer< 10 > buf;

I'm looking at using boost::enable_if_c but I don't understand how to get it working.

--Update-- I can't use any c++11 features, unfortunately

like image 609
ScaryAardvark Avatar asked Nov 28 '22 21:11

ScaryAardvark


1 Answers

Simply specialize the template to a state that cannot be instatiated:

template <>
class Buffer<0>;

That way the class cannot be constructed. Usage will result in: error: aggregate ‘Buffer<0> buf’ has incomplete type and cannot be defined

like image 107
Nobody moving away from SE Avatar answered Dec 20 '22 02:12

Nobody moving away from SE