c++模板:成员专业化问题
我正在尝试创建一个模板“AutoClass”,它创建具有任意成员集的任意类,例如:
AutoClass<int,int,double,double> a;
a.set(1,1);
a.set(0,2);
a.set(3,99.7);
std::cout << "Hello world! " << a.get(0) << " " << a.get(1) << " " << a.get(3) << std::endl;
现在我有一个带有工作“set”成员的 AutoClass:
class nothing {};
template < typename T1 = nothing, typename T2 = nothing, typename T3 = nothing,
typename T4 = nothing, typename T5 = nothing, typename T6 = nothing>
class AutoClass;
template <>
class AutoClass<nothing, nothing, nothing,
nothing, nothing, nothing>
{
public:
template <typename U> void set(int n,U v){}
};
template < typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6>
class AutoClass: AutoClass<T2,T3,T4,T5,T6>
{
public:
T1 V;
template <typename U> void set(int n,U v)
{
if (n <= 0)
V = v;
else
AutoClass<T2,T3,T4,T5,T6>::set(n-1,v);
}
};
并且我开始在实现相应的“get ”。这种方法无法编译:
template < typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6>
class AutoClass: AutoClass<T2,T3,T4,T5,T6>
{
public:
T1 V;
template <typename U> void set(int n,U v)
{
if (n <= 0)
V = v;
else
AutoClass<T2,T3,T4,T5,T6>::set(n-1,v);
}
template <typename W> W get(int n)
{
if (n <= 0)
return V;
else
return AutoClass<T2,T3,T4,T5,T6>::get(n-1);
}
template <> T1 get(int n)
{
if (n <= 0)
return V;
else
return AutoClass<T2,T3,T4,T5,T6>::get(n-1);
}
};
此外,似乎我需要为
I am attempting to create a template "AutoClass" that create an arbitrary class with an arbitrary set of members, such as:
AutoClass<int,int,double,double> a;
a.set(1,1);
a.set(0,2);
a.set(3,99.7);
std::cout << "Hello world! " << a.get(0) << " " << a.get(1) << " " << a.get(3) << std::endl;
By now I have an AutoClass with a working "set" member:
class nothing {};
template < typename T1 = nothing, typename T2 = nothing, typename T3 = nothing,
typename T4 = nothing, typename T5 = nothing, typename T6 = nothing>
class AutoClass;
template <>
class AutoClass<nothing, nothing, nothing,
nothing, nothing, nothing>
{
public:
template <typename U> void set(int n,U v){}
};
template < typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6>
class AutoClass: AutoClass<T2,T3,T4,T5,T6>
{
public:
T1 V;
template <typename U> void set(int n,U v)
{
if (n <= 0)
V = v;
else
AutoClass<T2,T3,T4,T5,T6>::set(n-1,v);
}
};
and I started to have problems implementing the corresponding "get". This approach doesn't compile:
template < typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6>
class AutoClass: AutoClass<T2,T3,T4,T5,T6>
{
public:
T1 V;
template <typename U> void set(int n,U v)
{
if (n <= 0)
V = v;
else
AutoClass<T2,T3,T4,T5,T6>::set(n-1,v);
}
template <typename W> W get(int n)
{
if (n <= 0)
return V;
else
return AutoClass<T2,T3,T4,T5,T6>::get(n-1);
}
template <> T1 get(int n)
{
if (n <= 0)
return V;
else
return AutoClass<T2,T3,T4,T5,T6>::get(n-1);
}
};
Besides, it seems I need to implement get for the <nothing, nothing, nothing, nothing, nothing, nothing>
specialization. Any Idea on how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,我更喜欢 Boost.Fusion< /a> 到 Boost.Tuple 因为我认为它支持模板元编程和运行时算法的更好混合。
例如,我想向您展示一个小奇迹:
使用起来真的很有趣:
嗯,我自己更喜欢按类建立索引而不是按索引,因为我可以传达含义并添加类型检查;)
First of all, I prefer Boost.Fusion to
Boost.Tuple
as it supports a better mixin of template metaprogramming and runtime algorithms I think.For example, I'd like to present you a little marvel:
It's really fun to use:
Well, I myself prefer indexing by classes than by indices, because I can convey meaning as well as adding type checking ;)
我是否可以推荐使用 Boost 库的广泛(且经过良好测试和跨平台)模板魔法类集?听起来您要寻找的是 boost: :元组。任何时候只要你可以不编写自己的代码(尤其是在使用模板的复杂情况下),你就应该使用别人的代码。
Might I recommend using the Boost library's extensive (and well-tested and cross-platform) set of template-magicky classes? It sounds like what you're looking for is boost::tuple. Any time you can get away with not writing your own code—especially in a complicated situation with templates—you should use someone else's.
正如其他人提到的,您可能应该能够通过重用 Boost 或其他地方的现有实现来达到您想要的目的。
如果您要做的事情无法使用这些方法完成,或者您很好奇:
实现
为了方便起见,利用 MPL 的简单方法可能如下所示:
用法:
请记住,这仍然可以用伪可变模板包装最终用户的便利。
As others mentioned, you probably should be able to get where you want by reusing existing implementations from Boost or elsewhere.
If you would be doing something that can't be done using those or if you're curious:
to the implementation
A simple approach, utilizing MPL for convenience could look something like this:
Usage:
Keep in mind that this can still be wrapped with pseudo-variadic templates for end-user-convenience.
您需要实现<无,无...>因为你的基本情况。考虑:
You need to implement for <nothing, nothing...> because of your base case. Consider: