Some aspects of Scala mixins can be satisfied using multiple (virtual) inheritance. Unfortunately, this often introduces more problems than it solves. Also, you can't mix and match superclasses on the fly a la:
val me = new Human with Coder with Musician
If you really, really want true mixins, you almost have to go with something like the template solution proposed in the answer by @Logan Capaldo.
Just to reiterate and extend what mentioned in previous emails let me first give you an example of how to implement the Scala Ordered trait in C++ and then let me show how you can mix-in an arbitrary number of "traits" at instantiation time.
Let's start first with the Ordered trait. If you are familiar with the Scala SDK you'll have noticed that there is an Ordered trait. This is used to provide total ordering by means of implementing a simple "compare" method. In C++, you could do the same as follows:
Then, to give ordering property to a C++ class you can do the following:
class MyOrderedType : public Ordered<MyOrderedType> {
public:
// Your ctor/dtors, methods
public:
int compare(const MyOrderedType& that);
};
Obviously you can mix-in as many "traits" as you want but if you do this way you can't add or remove traits at instantiation time. Is there a simple solution for this? Kind-of.
Have heard about the C++0x variadic templates? This provides a way of mixing-in as many traits as you want at template instantiation time.
The trick is simple, just declare your host class as follows:
template <typename... MIXINS>
class Host : public MIXINS... {
// Your implementation
};
What is the catch here? Well, the issue is that it is not possible to do something like this:
template <typename... MIXINS>
class Host : public MIXINS<HOST>... {
// Your implementation
};
Which in some cases would have been handy.
In any case, C++ has some basic mechanism that allow you to emulate some aspects of Scala's Mix-ins. What it can't do, however, is stacking behavior.
发布评论
评论(3)
Scala mixin 的某些方面可以使用多重(虚拟)继承来满足。 不幸的是,这往往带来的问题多于它解决的问题。 另外,你不能即时混合和匹配超类a la:
如果你真的,真的想要真正的混合,你几乎必须使用类似模板解决方案的东西@Logan Capaldo 在答案中提出。
Some aspects of Scala mixins can be satisfied using multiple (virtual) inheritance. Unfortunately, this often introduces more problems than it solves. Also, you can't mix and match superclasses on the fly a la:
If you really, really want true mixins, you almost have to go with something like the template solution proposed in the answer by @Logan Capaldo.
为了重申和扩展之前电子邮件中提到的内容,我首先给您一个如何在 C++ 中实现 Scala Ordered 特征的示例,然后让我展示如何在实例化时混合任意数量的“特征”。
让我们首先从 Ordered 特征开始。 如果您熟悉 Scala SDK,您会注意到有一个 Ordered 特征。 这用于通过实现简单的“比较”方法来提供总排序。 在 C++ 中,您可以执行以下操作:
然后,要为 C++ 类提供排序属性,您可以执行以下操作:
显然,您可以根据需要混合任意数量的“特征”,但如果您这样做,则可以' t 在实例化时添加或删除特征。 有一个简单的解决方案吗? 有点儿。
听说过 C++0x 可变参数模板吗? 这提供了一种在模板实例化时混合所需数量的特征的方法。
技巧很简单,只需按如下方式声明您的主机类:
这里有什么问题? 好吧,问题是不可能做这样的事情:
在某些情况下这会很方便。
无论如何,C++ 都有一些基本机制可以让您模拟 Scala Mix-ins 的某些方面。 然而,它不能做的是堆叠行为。
HTH。
Just to reiterate and extend what mentioned in previous emails let me first give you an example of how to implement the Scala Ordered trait in C++ and then let me show how you can mix-in an arbitrary number of "traits" at instantiation time.
Let's start first with the Ordered trait. If you are familiar with the Scala SDK you'll have noticed that there is an Ordered trait. This is used to provide total ordering by means of implementing a simple "compare" method. In C++, you could do the same as follows:
Then, to give ordering property to a C++ class you can do the following:
Obviously you can mix-in as many "traits" as you want but if you do this way you can't add or remove traits at instantiation time. Is there a simple solution for this? Kind-of.
Have heard about the C++0x variadic templates? This provides a way of mixing-in as many traits as you want at template instantiation time.
The trick is simple, just declare your host class as follows:
What is the catch here? Well, the issue is that it is not possible to do something like this:
Which in some cases would have been handy.
In any case, C++ has some basic mechanism that allow you to emulate some aspects of Scala's Mix-ins. What it can't do, however, is stacking behavior.
HTH.
不,但它可以用模板进行不同程度的伪造:
说实话,我还没有通过编译来测试它,但你应该明白这个想法。
No, but it can be faked to varying degrees with templates:
To be totally honest, I've haven't tested this by compiling it, but you should get the idea.