标签:c c11 templates variadic-templates inheritance
我想用可变参数模板编写线性叠加的抽象.为此,我想定义一个表示某种形式的operator()的基类型
template <typename Result, typename... Parameters>
class Superposable {
public:
typedef Result result_t;
void operator()(Result& result, const Parameters&...) const = 0;
};
然后从它继承当前的问题,例如像这样
class MyField : public Superposable<double, double, double> {
public:
void operator()(double& result, const double& p1, const double& p2) const {
result = p1 + p2;
}
};
然后我想编写一个抽象基类,它可以形成线性叠加,并将Superposable派生类作为模板参数来确定operator()的调用签名.我想有类似的东西
template<typename S> // where S must be inherited from Superposable
class Superposition {
private:
std::vector< std::shared_ptr<S> > elements;
public:
// This is the problem. How do I do this?
void operator()(S::result_t& result, const S::Parameters&... parameters) const {
for(auto p : elements){
S::result_t r;
p->operator()(r, parameters);
result += r;
}
}
};
这是我的问题:
>如何从Superposable派生类中读出类型信息以在Superposition中定义operator()?
>另外,是否有建议的方法来强制只能使用Superposable派生类作为参数调用Superposition?
>更好的解决方案当然是编写一个Superposition类,它不需要从基类派生MyField,而是直接解析MyField的operator().我能以某种方式这样做吗?
谢谢你的帮助!
解决方法:
首先,直接解决您的问题.
一些元编程样板:
template<class...>struct types{using type=types;};
一种hana风格的函数,它提取参数的类型,可选择根据以下内容从中进行提取的模板:
template<template<class...>class Z, class...Args>
constexpr types<Args...> extract_args( Z<Args...> const& ) { return {}; }
将以上内容包装为decltype以便于使用的别名:
template<template<class...>class Z, class T>
using extract_args_from = decltype( extract_args<Z>( std::declval<T>() ) );
Superposition的主要模板保留为空,其默认参数提取Superposable的类型参数:
template<class S, class Args=extract_args_from<Superposable, S>>
class Superposition;
然后是一个特殊化,它获得S(可能是基类)S的Superposable的Result和Parameters类型:
template<class S, class Result, class...Parameters>
class Superposition< S, types<Result, Parameters...>> {
live example.顺便说一下,你错过了一个虚拟的.
请注意,我不赞成你的设计 – 结果应该是返回值(自然地),make()虚拟似乎是一个坏主意(我改用CRTP,如果我真的需要隐藏则输入类型擦除特定的实施).
你可以删除Superposable的主体,它按原样工作:
public:
typedef Result result_t;
void operator()(Result& result, const Parameters&...) const = 0;
我至少推荐过这个.
下一步是取消继承并总结参数:
class MyField {
public:
double operator()(const double& p1, const double& p2) const {
return p1 + p2;
}
};
template<typename S> // where S must be inherited from Superposable
class Superposition {
private:
std::vector< std::shared_ptr<S> > elements;
public:
template<class...Parameters,
class R=std::result_of_t<S&(Parameters const&...)>
>
R operator()(const Parameters&... parameters) const {
R ret = {};
for(auto p : elements){
ret += (*p)(parameters...);
}
return ret;
}
};
它具有完美转发的所有缺陷,但也具有所有优点.
的std :: result_of_t<?>是C 14,但在C 11中将typename std :: result_of<?> :: type替换为drop-in.
标签:c,c11,templates,variadic-templates,inheritance 来源: https://codeday.me/bug/20190823/1694877.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。