假设我是某个模板库 (CTL
) 的用户,它定义了一个名为 Hector
的模板,
template <class T>
class Hector {...};
并且在其文档中给出了有关 Hector 的许多保证
模板行为。
但随后它还为某种类型定义了特化 Cool
template <>
class Hector<Cool> {....};
特化的目的是更加优化的实现 Hector
,但不幸的是由于这种优化很多保证了 >赫克托
被侵犯。
目前我确实不需要优化,我宁愿保留 Hector
的所有保证。有什么方法可以在不更改库代码的情况下(CTL
是一个非常受人尊敬的库,你知道),绕过专业化?有办法吗?也许写某种包装?任何事物?我只是想让编译器以正常的、非优化的方式为 Hector
生成代码,并提供所有保证。
Suppose I am a user of a Certain Template Library (CTL
) which defines a template, named, say, Hector
template <class T>
class Hector {...};
And in its documentation it gives many guarantees about Hector
template behavior.
But then it also defines a specialization for a certain type Cool
template <>
class Hector<Cool> {....};
The purpose of the specialization is a more optimized implementation of Hector
, but unfortunately because of this optimization many guarantees of Hector
are violated.
Currently I really don't need the optimization, I'd rather preserve all the guarantees of Hector
. Is there any way I could, without changing the library code (CTL
is a highly respectable library, you know), circumvent the specialization? Any way at all? Maybe write some sort of wrapper? Anything? I just want to the compiler to generate code for Hector<Cool>
in a normal, non-optimized way, with all the guarantees.
发布评论
评论(5)
您是否可以使用没有不需要的专业化的相关模板
Reque
?否则,我认为您需要为Cool
创建一个包装器,以便不使用专业化。Are you able to use the related template
Reque
that doesn't have the undesired specialization? Otherwise I think you'd need to create a wrapper forCool
so that the specialization isn't used.您可以将 Cool 包装在虚拟类型中,以防止模板对其进行专门化。
You could wrap cool in a dummy type to prevent the template from specializing it.
不。即使可以以某种深奥的方式完成,也不要这样做。规避语言功能应该引起警报。
您必须包装该值或使用不同的类型,例如
char
而不是bool
(它们的行为类似),给出std::vector
而不是std::vector
。No. And even if it can be done in some esoteric fashion, don't. Circumventing language features should set off an alarm.
You have to wrap the value or use a different type like
char
instead ofbool
(they behave similarly), givingstd::vector<char>
instead ofstd::vector<bool>
.这里有一个通用的伪装器:
现在你可以说
Hector>
。根据Xeo的改进版本:
Here's a little generic disguiser:
Now you can say
Hector<Drool<Cool>>
.Improved version according to Xeo:
标准某些实现"my_hector.h"
#include
与#include "my_hector.h"
[编辑@Xeo;-)]
standardcertain implementation"my_hector.h"
#include <hector>
with#include "my_hector.h"
[ Edit for @Xeo ;-) ]