规避模板专业化

发布于 2024-11-17 10:41:56 字数 590 浏览 1 评论 0 原文

假设我是某个模板库 (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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花伊自在美 2024-11-24 10:41:56

您是否可以使用没有不需要的专业化的相关模板 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 for Cool so that the specialization isn't used.

吃不饱 2024-11-24 10:41:56

您可以将 Cool 包装在虚拟类型中,以防止模板对其进行专门化。

You could wrap cool in a dummy type to prevent the template from specializing it.

尤怨 2024-11-24 10:41:56

不。即使可以以某种深奥的方式完成,也不要这样做。规避语言功能应该引起警报。

您必须包装该值或使用不同的类型,例如 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 of bool (they behave similarly), giving std::vector<char> instead of std::vector<bool>.

御守 2024-11-24 10:41:56

这里有一个通用的伪装器:

template <typename T>
struct Drool
{
  Drool(T d) : b(d) { }
  inline operator T() const { return b; }
  inline Drool<T> & operator=(T d) { b = d; return *this; }
private:
  T b;
};

现在你可以说 Hector>


根据Xeo的改进版本:

template <typename T>
struct Drool
{
  Drool(const T & d) : b(d) { }
  Drool(Drool && o) = default;

  inline operator const T & () const { return b; }
  inline operator       T & ()       { return b; }

private:
  T b;
};

Here's a little generic disguiser:

template <typename T>
struct Drool
{
  Drool(T d) : b(d) { }
  inline operator T() const { return b; }
  inline Drool<T> & operator=(T d) { b = d; return *this; }
private:
  T b;
};

Now you can say Hector<Drool<Cool>>.


Improved version according to Xeo:

template <typename T>
struct Drool
{
  Drool(const T & d) : b(d) { }
  Drool(Drool && o) = default;

  inline operator const T & () const { return b; }
  inline operator       T & ()       { return b; }

private:
  T b;
};
宁愿没拥抱 2024-11-24 10:41:56
  1. 打开标准某些实现
  2. Ctrl+A
  3. Ctrl+C
  4. 创建一个新文件名为 "my_hector.h"
  5. Ctrl+V
  6. 删除专门化
  7. 搜索并替换 #include #include "my_hector.h"
    [编辑@Xeo;-)]
  8. 重命名以两个前导下划线后跟一个小写字母开头的标识符,以及以一个前导下划线后跟一个大写字母开头的所有标识符。
  1. Open the standard certain implementation
  2. Ctrl+A
  3. Ctrl+C
  4. Create a new file called "my_hector.h"
  5. Ctrl+V
  6. Remove the specialisation
  7. Search and replace #include <hector> with #include "my_hector.h"
    [ Edit for @Xeo ;-) ]
  8. Rename identifiers that begin with two leading underscores followed by a lowercase letter, and all identifiers that begin with a single leading underscore following by an uppercase letter.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文