C++ 中的 Mixins、可变参数模板和 CRTP;
场景如下:我想要一个可以有可变数量的 mixins 的主机类(使用可变参数模板并不太难 - 例如参见 http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.144)。但是,我还希望 mixin 由主机类参数化,以便它们可以引用其公共类型(使用 CRTP 习惯用法)。 当尝试混合两者时,问题就出现了——我不清楚正确的语法。 例如,以下代码无法使用 g++ 4.4.1 进行编译:
template <template<class> class... Mixins>
class Host : public Mixins<Host<Mixins>>... {
public:
template <class... Args>
Host(Args&&... args) : Mixins<Host>(std::forward<Args>(args))... {}
};
template <class Host> struct Mix1 {};
template <class Host> struct Mix2 {};
typedef Host<Mix1, Mix2> TopHost;
TopHost *th = new TopHost(Mix1<TopHost>(), Mix2<TopHost>());
出现错误:
tst.cpp: In constructor ‘Host<Mixins>::Host(Args&& ...) [with Args = Mix1<Host<Mix1, Mix2> >, Mix2<Host<Mix1, Mix2> >, Mixins = Mix1, Mix2]’:
tst.cpp:33: instantiated from here
tst.cpp:18: error: type ‘Mix1<Host<Mix1, Mix2> >’ is not a direct base of ‘Host<Mix1, Mix2>’
tst.cpp:18: error: type ‘Mix2<Host<Mix1, Mix2> >’ is not a direct base of ‘Host<Mix1, Mix2>’
是否有人有将可变参数模板与 CRTP 混合的成功经验?
Here's the scenario: I'd like to have a host class that can have a variable number of mixins (not too hard with variadic templates--see for example http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.144). However, I'd also like the mixins to be parameterized by the host class, so that they can refer to its public types (using the CRTP idiom).
The problem arises when trying to mix the two--the correct syntax is unclear to me.
For example, the following code fails to compile with g++ 4.4.1:
template <template<class> class... Mixins>
class Host : public Mixins<Host<Mixins>>... {
public:
template <class... Args>
Host(Args&&... args) : Mixins<Host>(std::forward<Args>(args))... {}
};
template <class Host> struct Mix1 {};
template <class Host> struct Mix2 {};
typedef Host<Mix1, Mix2> TopHost;
TopHost *th = new TopHost(Mix1<TopHost>(), Mix2<TopHost>());
With the error:
tst.cpp: In constructor ‘Host<Mixins>::Host(Args&& ...) [with Args = Mix1<Host<Mix1, Mix2> >, Mix2<Host<Mix1, Mix2> >, Mixins = Mix1, Mix2]’:
tst.cpp:33: instantiated from here
tst.cpp:18: error: type ‘Mix1<Host<Mix1, Mix2> >’ is not a direct base of ‘Host<Mix1, Mix2>’
tst.cpp:18: error: type ‘Mix2<Host<Mix1, Mix2> >’ is not a direct base of ‘Host<Mix1, Mix2>’
Does anyone have successful experience mixing variadic templates with CRTP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下似乎有效。我在继承的 mixin 类中添加了 Mixins...,从而就地扩展了参数包。在
Host
模板主体之外,必须指定Host
的所有模板参数,以便Mixins...
达到目的。在正文中,只需Host
就足够了,无需拼写出其所有模板参数。有点手短。The following seems to work. I added
Mixins...
in the inherited mixin classes which expands the parameter pack inplace. Outside the body ofHost
template, all template parameters ofHost
must be specified soMixins...
serves the purpose. Inside the body, justHost
is sufficient no need to spell out all its template parameters. Kind of a short hand.