I’ve got this queue class, actually, several that suffer from the same issue – performance will be poor if compiled with a type that has a lengthy copy ctor – the queue is locked during push/pop and the longer it is locked, the greater the chance of contention. It would be useful if the class would not compile if some developer tried to compile it with a 10MB buffer class, (instead of a pointer to it).
It seems that there is no easy way to restrict template parameters to base classes or any other type.
Is there some bodge I can use so that my class will not compile if the parameter is not a pointer to a class instance?
You can do this in several ways. As another answer points out you can do it with a static_assert (preferably from C++11/Boost although you can roll your own), although I’d recommend checking if it is actually a pointer and not just relying on the size. You can either roll your own or use an existing trait (available in C++11 too) depending on what system you’re using:
But that raises a bigger point. If your requirement is that you only ever point to things, why not interpret the template parameter like that to begin with? E.g. make your container:
instead of allowing people to specify non-pointer types in the first place?
Finally if you don’t want to go down the static_assert road you can just specialise the container for pointer types only and not implement it for non-pointers, e.g.:
This still requires explicitly making the type a pointer, still causes compile time failure for non-pointer types, but doesn’t need a static_assert or way of determining if a type is a pointer.