I'd like to prevent all implicit template instantiations for a specific templated class in order to prevent it from being instantiated into every translation unit.
It looks like my options are:
So I need something in-between. It would be nice to have:
extern template class Foo; // suppress all implicit instantiations of Foo
(Note the lack of template parameter(s).) Any ideas?
There are ways to restrict the types you can use inside a template you write by using specific typedefs inside your template. This will ensure that the compilation of the template specialisation for a type that does not include that particular typedef will fail, so you can selectively support/not support certain types.
To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>(float original); Template arguments may be omitted when the compiler can infer them.
Unless a template specialization has been explicitly instantiated or explicitly specialized, the compiler will generate a specialization for the template only when it needs the definition. This is called implicit instantiation.
you can use std::enable_if
which does exactly this with the combination of std::is_same
:
template <class T , typename = std::enable_if <!std::is_same<T,Foo>::value,T>::type >
class MyClass{
//...
};
now myClass won't be compiled for Foo
type.
I would say that the answer to your question is using C++ new type traits to assert the instantiations in your constructor:
static_assert(std::is_same<TInstantiation, [your-predefined-type]> || std::is_same<TInstantiation, [your-predefined-type2]> /*And so on...*/, "Classname can not be instantiated using this type!");
It's all guaranteed to resolve at compile time :)
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