基于继承类的模板特化
我想让这个专业化而不改变主要。 是否有可能根据其基类来专门化某些东西? 但愿如此。
-编辑-
我将有几个继承自 SomeTag 的类。 我不想为他们每个人编写相同的专业化。
class SomeTag {};
class InheritSomeTag : public SomeTag {};
template <class T, class Tag=T>
struct MyClass
{
};
template <class T>
struct MyClass<T, SomeTag>
{
typedef int isSpecialized;
};
int main()
{
MyClass<SomeTag>::isSpecialized test1; //ok
MyClass<InheritSomeTag>::isSpecialized test2; //how do i make this specialized w/o changing main()
return 0;
}
I want to make this specialized w/o changing main. Is it possible to specialize something based on its base class? I hope so.
-edit-
I'll have several classes that inherit from SomeTag. I don't want to write the same specialization for each of them.
class SomeTag {};
class InheritSomeTag : public SomeTag {};
template <class T, class Tag=T>
struct MyClass
{
};
template <class T>
struct MyClass<T, SomeTag>
{
typedef int isSpecialized;
};
int main()
{
MyClass<SomeTag>::isSpecialized test1; //ok
MyClass<InheritSomeTag>::isSpecialized test2; //how do i make this specialized w/o changing main()
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 C++20 中的概念和 require 关键字是一种更简单、更具表现力的方法,而无需像 C++11 中那样引入冗余的布尔非类型模板参数:
Using concepts and the requires keyword from C++20 is an even simpler and more expressive way to do this without having to introduce a redundant boolean non-type template parameter like in C++11:
使用 C++-20 更新概念:
我 2014 年的帖子,使用 C++-11:
Update for concepts, using C++-20:
My post from 2014, using C++-11:
本文描述了一个巧妙的技巧:http://www.gotw.ca/publications/ mxc++-item-4.htm
这是基本思想。 您首先需要一个 IsDerivedFrom 类(这提供了运行时和编译时检查):
然后您的 MyClass 需要一个可能专门化的实现:
并且 MyClass 实际上看起来像:
然后您的 main 就可以了:
This article describes a neat trick: http://www.gotw.ca/publications/mxc++-item-4.htm
Here's the basic idea. You first need an IsDerivedFrom class (this provides runtime and compile-time checking):
Then your MyClass needs an implementation that's potentially specialized:
and MyClass actually looks like:
Then your main will be fine the way it is:
嗯,上面答案中的文章发表于 2002 年 2 月。虽然它有效,但今天我们知道还有更好的方法。 或者,您可以使用
enable_if
:Well, the article in the answer above appeared in February 2002. While it works, today we know there are better ways. Alternatively, you can use
enable_if
:就您而言,我看到的唯一方法是显式专门化
MyClass
为InheritSomeTag
。 然而,SeqAn 论文提出了一种称为“模板子类化”的机制,执行您想要的操作 - 尽管使用不同的继承语法,因此代码与您当前的main
函数不兼容。这看起来确实很奇怪,而且非常麻烦,但它允许真正的继承机制,具有在编译时执行的多态函数。 如果您想查看实际效果,请查看一些 SeqAn 示例 。
话虽这么说,我相信 SeqAn 是一个特例,没有多少应用程序会从这种极其困难的语法中受益(破译与 SeqAn 相关的编译器错误是 *ss 中真正的痛苦!)
In your case, the only way that I see would be to explicitly specialize
MyClass
forInheritSomeTag
. However, the SeqAn paper proposes a mechanism called “template sublassing” that does what you want – albeit with a different inheritance syntax, so the code isn't compatible with your currentmain
function.This certainly looks strange and is very cumbersome but it allows a true inheritance mechanism with polymorphic functions that is executed at compile time. If you want to see this in action, have a look at some SeqAn examples.
That being said, I believe that SeqAn is a special case and not many applications would profit from this extremely difficult syntax (deciphering SeqAn-related compiler errors is a real pain in the *ss!)