我希望阻止特定模板类的所有隐式模板实例化,以防止它被实例化到每个翻译单元。
看起来我的选择是:
所以我需要介于两者之间。如果有这样的话会很高兴:
extern template class Foo; // suppress all implicit instantiations of Foo
(注意缺少模板参数。)任何想法?
答案 0 :(得分:1)
我想说你的问题的答案是使用C ++新类型特征来断言你的构造函数中的实例化:
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!");
所有这些都保证在编译时解决:)
答案 1 :(得分:1)
您可以像使用非模板类一样拆分类。只需将声明放入标题并将其包含在任何地方,并将实现放入.inl / .i文件中,并将其包含在您希望实例化模板的位置。或者,使用编译时开关使用单个文件执行此操作 - 但您必须将声明和实现分开。
答案 2 :(得分:1)
您可以使用std::enable_if
与std::is_same
:
template <class T , typename = std::enable_if <!std::is_same<T,Foo>::value,T>::type >
class MyClass{
//...
};
Foo
现在不会为 <head>
<script src="jquery-1.11.3.min.js"></script>
<script src="jquery-hex-colorpicker.js"></script>
<script src="common.js"></script>
<script src="paintbrush.js"></script>
</head>
<body>
<input type="text" id="color-picker1" placeholder="Click for Demo" />
<img src="/images/Layer2.png" class="filter-tint" data-pb-tint-opacity="0.5" data-pb-tint-colour="#F69023" id="image">
</body>
类型编译myClass。