C# Typelist 类似于带有层次结构生成器的 Loki::Typelist
我喜欢 Loki 的 C++ HierarchyGenerator,并且想在 C# 中做同样的事情。
我最终想要的是一个在给定类型列表中每个参数都有一个虚函数的类。
我想要转换的 C++ 代码:
template <class T>
class SenderV
{
public:
virtual void Send(T t) = 0;
};
template <int i>
class Foo // Just to make it easy to show typelist, it's not interesting.
{ /* doIt definition */ };
typedef TYPELIST_2(Foo<1>,Foo<2>) FooSendables;
template <typename TList=FooSendables>
class FooSend : public Loki::GenScatterHierarchy <TList,SenderV>
{
public:
void Send(Foo<1> f) {f.doIt();std::cout<<"Sending Foo1."<<std::endl;};
void Send(Foo<2> f) {f.doIt();std::cout<<"Sending Foo2."<<std::endl;};
};
C# 格式。如果您不熟悉 Loki,上面的 FooSend 类将默认为:
class FooSend : SenderV<Foo<1> >, SenderV<Foo<2> >//including every type in TList
{ /*... as above */};
但是当给出新的 TList 时,它将是基于 TList 中的类型的不同层次结构。
我也对 Loki 中的 GenLinearHierarchy 感兴趣(如果它存在的话)。
我总是可以尝试在两种语言之间进行翻译,但我不太热衷于尝试这样做,因为我是 C# 新手,只想完成我的工作,而不是学习模板和泛型之间的细微差别。
I loved Loki's C++ HierarchyGenerator and want to do the same in C#.
What I want in the end is a class that has a virtual function per argument in a given typelist.
C++ code I'd like to convert:
template <class T>
class SenderV
{
public:
virtual void Send(T t) = 0;
};
template <int i>
class Foo // Just to make it easy to show typelist, it's not interesting.
{ /* doIt definition */ };
typedef TYPELIST_2(Foo<1>,Foo<2>) FooSendables;
template <typename TList=FooSendables>
class FooSend : public Loki::GenScatterHierarchy <TList,SenderV>
{
public:
void Send(Foo<1> f) {f.doIt();std::cout<<"Sending Foo1."<<std::endl;};
void Send(Foo<2> f) {f.doIt();std::cout<<"Sending Foo2."<<std::endl;};
};
in C#. If you aren't familiar with Loki, the FooSend class above would default to:
class FooSend : SenderV<Foo<1> >, SenderV<Foo<2> >//including every type in TList
{ /*... as above */};
But when a new TList is given, it'd be a different Hierarchy based on the Types in the TList.
I'm also interested in a GenLinearHierarchy in Loki if it exists out there.
I could always try to translate between the languages, but I'm not a big fan of trying that as I'm new to C# and just want to do my work, not learn the subtle difference between templates and generics.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道 Loki,但我觉得你使用了多重继承。
c# 不支持多重继承,而我在使用 c# 多年中学到的是,我不会错过它。
I have no idea of Loki but it looks for me that you use multiple inheritance.
c# does not support multiple inheritance and what I have learned many years working with c# is that I do not miss it.
使用 t4:
我无法按照我想要的方式获得格式(并且无论如何,t4 格式总是很难看),但这解决了我的问题。
上面的代码产生:
这符合我的需要。
using t4:
I can't get the formatting the way I'd like (and, regardless, the t4 formatting is always going to be ugly), but this solves my problem.
The code above produces:
Which fits my needs.