如何解包在mixins宿主类中传递的一组元组(最终被转发给mixins)
how to unpack a set of tuples passed in a mixins Host class (which ultimately are fowarded to the mixins)
我有以下代码,可以很好地将各种std::tuples
转发到我的" basessensor "主机类的各种混合。
#include <iostream> // std::cout std::endl
#include <string> // std::string
#include <map> // std::map
#include <tuple> // std::map
using namespace std;
struct BaseSensorData
{
BaseSensorData(const string & _sensorName)
: sensorName(_sensorName)
{}
const string sensorName;
};
class TrendSensor //mixin for BaseSensor
{
public:
TrendSensor( BaseSensorData * bsd , std::tuple<size_t , double > && tp)
{
cout << "TrendSensor: " << std::get<0>(tp) << " " << bsd->sensorName << " " << std::get<1>(tp) << endl;
}
};
typedef struct{ double trough; double peak; } Edges;
class EdgeSensor //mixin for BaseSensor
{
public:
EdgeSensor( BaseSensorData * bsd , std::tuple<size_t , double > && tp)
{
cout << "EdgeSensor: " << std::get<0>(tp) << " " << bsd->sensorName << " " << std::get<1>(tp) << endl;
}
};
class CO2Threshold// : public ThresholdData
{
std::map<std::string , double>thresholds;
public:
CO2Threshold( const double & _toxicThres , const double & _zeroThres , const double & )
{
thresholds["toxic"] = _toxicThres;
thresholds["zero"] = _zeroThres;
cout << "CO2Threshold: " << _toxicThres << " " << thresholds["zero"] << endl;
}
};
class O2Threshold// : public ThresholdData
{
std::map<std::string , double>thresholds;
public:
O2Threshold( const double & _toxicThres , const double & _lowThres , const double & _elecChemThres )
{
thresholds["toxic"] = _toxicThres;
thresholds["low"] = _lowThres;
thresholds["elecchem"] = _elecChemThres;
cout << "O2Threshold: " << _toxicThres << " " << thresholds["low"] << " " << thresholds["elecchem"] << endl;
}
};
template<typename ThresholdMixin> //CO2Threshold , O2Threshold , or others ...
class ThresholdSensor : public ThresholdMixin //mixin for BaseSensor
{
public:
ThresholdSensor ( BaseSensorData * bsd
, std::tuple<size_t , double , double , double , double> && tp)
: ThresholdMixin
( std::get<2>(tp)
, std::get<3>(tp)
, std::get<4>(tp)
)
{
cout << "ThresholdSensor: " << std::get<0>(tp) << " " << bsd->sensorName << " "
<< std::get<1>(tp) << " " << std::get<2>(tp) << " " << std::get<3>(tp) << " " << std::get<4>(tp)
<< endl;
}
};
template<typename ... SensorType>
class BaseSensor : public BaseSensorData , public SensorType ... //to my BaseSensor class
{
size_t windowSize;
public:
template<typename ... Args>
BaseSensor(const string& _sensorName , Args && ... args)
: BaseSensorData(_sensorName)
, SensorType( this , std::forward<Args>(args) )...
, windowSize(0U)
{
std::tuple<Args...> arr[sizeof...(SensorType)] = { args ... }; //line A
/*for (size_t i = 0; i < sizeof...(SensorType); ++i)
{
for (size_t i = 0; i < sizeof...(Args); ++i)
{
// do stuff
}
}*/
cout << "BaseSensor: " << /*_windowSize << " " << */sensorName << " " << endl;
}
};
int main() {
BaseSensor<ThresholdSensor<O2Threshold> , TrendSensor, EdgeSensor>bs
{
string("testSensor")
, std::make_tuple<size_t , double , double , double , double>( 54U , 2.5f , 10000.3f , 400.5f , 200.3f )
, std::make_tuple<size_t , double >( 53U , 6.5f )
, std::make_tuple<size_t , double >( 52U , 3.5f )
};
/*BaseSensor<ThresholdSensor<CO2Threshold> , TrendSensor, EdgeSensor>bs5
{
string("testSensor")
, std::make_tuple<size_t , double , double , double , double>( 54U , 2.5f , 10000.3f , 400.5f , 200.3f )
, std::make_tuple<size_t , double >( 53U , 6.5f )
, std::make_tuple<size_t , double >( 52U , 3.5f )
};*/
//BaseSensor<ThresholdSensor<CO2Threshold> > bs2{string("test"),11U , 12.5f};
//BaseSensor<TrendSensor> bs3{string("test"), 8U , 10.5f};
//BaseSensor<EdgeSensor> bs4{string("test"), 6U , 18.5f};
}
所以在"basessensor"中,所有混合的元组(即"SensorTypes…")都通过"Args…"参数包转发。这工作得很好,但我需要在BaseSensor::BaseSensor()
体访问那些元组。然而,这样做的语法使我迷惑不解。
如何同时展开SensorType ...
和Args ...
,并根据元组对"args"进行分类?使用上面的代码,我在g++ v4.7.1和c++11中启用了以下内容:
varcon.cpp:104:64: error: could not convert ‘args#0’ from ‘std::tuple<long unsigned int, double, double, double, double>’ to ‘std::tuple<std::tuple<long unsigned int, double, double, double, double>, std::tuple<long unsigned int, double>, std::tuple<long unsigned int, double> >’
varcon.cpp:104:64: error: could not convert ‘args#1’ from ‘std::tuple<long unsigned int, double>’ to ‘std::tuple<std::tuple<long unsigned int, double, double, double, double>, std::tuple<long unsigned int, double>, std::tuple<long unsigned int, double> >’
varcon.cpp:104:64: error: could not convert ‘args#2’ from ‘std::tuple<long unsigned int, double>’ to ‘std::tuple<std::tuple<long unsigned int, double, double, double, double>, std::tuple<long unsigned int, double>, std::tuple<long unsigned int, double> >’
varcon.cpp:108:59: error: incomplete type ‘std::tuple_size<std::tuple<std::tuple<long unsigned int, double, double, double, double>, std::tuple<long unsigned int, double>, std::tuple<long unsigned int, double> >&>’ used in nested name specifier
您想从下面的说明中获得什么?
std::tuple<Args...> arr[sizeof...(SensorType)] = { args ... };
Args...
包是不同std::tuple
类型的路径;所以你的arr
是一个元组的元组的数组;所以你必须给它一个由元组的元组组成的序列。但是args...
包它只是一个元组的包,而不是元组的元组。
所以clang++
的错误tmp_002-11,14,gcc,clang.cpp:93:64: error: no viable conversion from
'tuple<unsigned long, double, double, double, double>' to
'tuple<std::tuple<unsigned long, double, double, double, double>,
std::tuple<unsigned long, double>, std::tuple<unsigned long, double>,
(no argument), (no argument)>'
我想你可以把数组部分扔掉,把arr
定义为
std::tuple<Args...> arr { args ... };
接下来,如果想访问单个元素,可以这样写
std::get<0>(std::get<1>(arr));
,其中第一个std::get
(内部的)访问元组(args...
中的一个),第二个std::get
(更外部的)访问元组的元素(size_t
或double
)。
相关文章:
- C++:TypeDef使用元组
- Pybind11:将元组列表从Python传递到C++
- 重载元组索引运算符-C++
- 在C++中,如何通过几种类型从元组中选择多个元素
- 将fold表达式与std::一起用于两个元组
- std::ranges::elements_view,用于自定义类似元组的数据
- 将元组的向量转换/构造为堆
- 专用于 std 元组的模板,而无需用户执行remove_cvref
- 将元组的向量构造成堆
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 将元组类型扩展为可变参数模板?
- 时间复杂度 当具有复合数据类型(如元组或对)时?
- 类内部和外部静态 constexpr 元组之间的差异
- 可变参数模板与使用元组在参数中添加不同的数据对
- 访问和打印元组中的数据,并使用 C++14 使用模板函数显示数据
- boost::包含提升单元的元组的哈希值
- 我正在寻找一种优雅的方式来从元组向量创建tuple_element向量
- 如何在可变参数模板函数中遍历可变参数元组?
- 如何使用 SML 随机生成八进制元组
- 如何解包在mixins宿主类中传递的一组元组(最终被转发给mixins)