如何解决如何使用C ++ 11中的可变参数非类型模板参数解决此问题?
enum Enum
{
e0,e1,e2
};
int translate(Enum e)
{
//...
}
int translate(Enum e,int index)
{
//...
}
class A
{
public:
template<typename... Ts>
A(Ts... ts)
{
//...
}
};
template<Enum... es>
class B
{
public:
static std::shared_ptr<A> getA()
{
//for example,use "int translate(Enum e)"
//return std::make_shared<A>(translate(es)...);
//use "int translate(Enum e,int index)" "index" like the index in "for(int index = 0; index < n; ++index)"
//how to writer?
}
};
这是关于可变参数非类型模板参数;我想用C ++ 11来解决它。
例如:
std::make_shared<A>(translate(e1,0),translate(e2,1),translate(e3,2)) std::make_shared<A>(translate(e1,1)) std::make_shared<A>(translate(e3,translate(e0,1))
解决方法
这是使用std::integer_sequence
的解决方案。这是C ++ 14的功能,但可以移植到C ++ 11 do exist(尚未使用过,无法保证其质量)。
template<Enum... es>
class B
{
template <int... Is>
static std::shared_ptr<A> getAHelper(std::integer_sequence<Is...>) {
return std::make_shared<A>(translate(es,Is)...);
}
public:
static std::shared_ptr<A> getA()
{
return getAHelper(std::make_integer_sequence<sizeof...(es)>{});
}
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。