[英]Prevent all instantiations of a template class - including supported types
If I have a template class MyClass<T>
and if I explicitly instantiate for int
and float
(in a cpp
file) then I can use extern template class MyClass<int>
and extern template class MyClass<float>
to prevent any compilation unit encountering this class from instantiating it for int
and float
unnecessarily. 如果我有模板类
MyClass<T>
并且如果我显式实例化了int
和float
(在cpp
文件中),则可以使用extern template class MyClass<int>
和extern template class MyClass<float>
来防止遇到任何编译单元此类从不必要的实例化为int
和float
。 Of course, for any other type, the class will still get instantiated. 当然,对于任何其他类型,该类仍将实例化。
Now I have a class MyClass2<T>
which only works with int
, float
, char
, double
, short
and their unsigned variants where applicable. 现在,我有一个类
MyClass2<T>
,它仅适用于int
, float
, char
, double
, short
和它们的无符号变体。 Since I know all the types beforehand, the definitions of all the methods in this class are in the cpp
file. 由于我事先知道所有类型,因此此类中所有方法的定义都在
cpp
文件中。 This is also where I explicitly instantiate MyClass2<T>
for all the above mentioned types. 这也是我为上述所有类型显式实例化
MyClass2<T>
的地方。 In the header, I have static_assert
preventing the user from creating MyClass2<T>
with an unsupported type. 在标题中,我有
static_assert
防止用户使用不受支持的类型创建MyClass2<T>
。
Is there any way to completely prevent MyClass2<T>
from instantiating (eg extern template class MyClass2;
although I know that doesn't work) for all types including supported types ? 有什么方法可以完全阻止
MyClass2<T>
实例化所有类型( 包括支持的类型 )(例如, extern template class MyClass2;
尽管我知道这不起作用)? Like a catch all for extern template
? 像所有
extern template
? I want to avoid typing out extern template class MyClass2<int>
for all the supported types. 我想避免为所有受支持的类型键入
extern template class MyClass2<int>
。
Considering I have already explicitly instantiated the class for those types, it seems not only redundant but for big projects (like the one I am working on) yet another few lines that I need to maintain each time a new type is added. 考虑到我已经为这些类型显式实例化了该类,这似乎不仅是多余的,而且对于大型项目(如我正在研究的项目)而言,每次添加新类型时我都需要维护的另外几行。
You can use SFINAE something like following 您可以使用SFINAE如下所示
template<typename T>
using value_type = typename std::enable_if<
std::is_same<float, T>::value ||
std::is_same<double, T>::value
//...other supported types goes here
>::type ;
template<class T, class Enable = void>
class MyClass2 ;
template<class T>
class MyClass2<T, value_type<T> >
{
};
MyClass2<float> t;
MyClass2<int> t1; // Error !
This honestly sounds like the job for a macro: 老实说,这听起来像是宏的工作:
// explicit_instantiations.h
#ifndef PERFORM_INSTANTIANTION
#define EXTERNALLY_INSTANTIATED extern
#else
#define EXTERNALLY_INSTANTIATED
#endif
EXTERNALLY_INSTANTIATED template class MyClass<int>;
EXTERNALLY_INSTANTIATED template class MyClass<float>;
// etc.
// header
#include "explicit_instantiations.h"
// cpp file
#define PERFORM_INSTANTIATION
#include "explicit_instantiations.h"
What about: 关于什么:
template <typename T>
struct Base {};
template <typename T>
struct Supported;
template <>
struct Supported<int> : public Base<int>
{};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.