odi*_*erd 12 c++ boost boost-parameter c++14
我目前正在使用boost.parameter和一些工厂函数,编译时间越来越多.
目前我有一个像这样的共同模式:
auto thing = makeThing(property1 = foo::bar, "myThing"_thingName);
Run Code Online (Sandbox Code Playgroud)
其中makeThing
有30个参数,其中大部分都是默认值.我想保留"命名参数"语法以及按类型而不是按位置匹配参数的能力.
如何在不改变工厂调用站点的语法的情况下获得更好的编译速度?
注意:根据boost.MPL速度和强度速度之间的差异来判断,如果现代元编程技术用于boost.parameter等效,那么编译时应该至少有一个数量级的改进.
更新:这是我正在做的一个简略示例:在裸机嵌入式上下文中,我根据基于策略的类设计习惯用不同的外围设备抽象为复杂的模板类.每个类在编译时都会获取大量配置信息,并且只使用所需的功能(不能依赖优化器来去除未使用的东西,因为所有SFR交互都是可观察的,因此是不稳定的,因此不允许).
这些基于策略的类对于用户来说是非常丑陋的,并且如果大多数嵌入式用户在公共界面中看到<,那么我会使用boost.parameter创建一个性感的工厂,以便他们可以传递所有类型编码的愿望(像hana风格)我生成的类作为本地静态连接到所需的ISR并传回一个句柄.
namespace usb
{
BOOST_PARAMETER_NAME(hw)
BOOST_PARAMETER_NAME(vid)
BOOST_PARAMETER_NAME(pid)
BOOST_PARAMETER_NAME(device_class)
BOOST_PARAMETER_NAME(max_packet_ep0)
BOOST_PARAMETER_NAME(max_packet)
BOOST_PARAMETER_NAME(packet_pool_size)
BOOST_PARAMETER_NAME(device_description)
BOOST_PARAMETER_NAME(device_calss_description)
BOOST_PARAMETER_NAME(can_power_down)
BOOST_PARAMETER_NAME(stall_supported)
BOOST_PARAMETER_NAME(setup_packet_timeout)
//...
BOOST_PARAMETER_FUNCTION(
(usb_handle),
make,
tag,
(required(hw, *))
(optional
(vid, *, 0x6001_ci)
(pid, *, 0x1234_ci)
(device_class, *, cdc_class{})
(max_packet_ep0, *, 8_ci)
(max_packet, *, 64_ci)
(packet_pool_size, *, 12_ci)
(device_description, *, "")
(device_calss_description, *, "")
(can_power_down, *, 0_ci)
(stall_supported, *, 0_ci)
(setup_packet_timeout, *, 100_ci)
)
)
{
// makes a local static policy based class taylored at compile time
// to support only the specified features
return{}; //returns a handle to the local static
}
}
Run Code Online (Sandbox Code Playgroud)
大多数工厂都有10-25个参数,预处理时间似乎是杀手锏.无论用户是否实际调用该功能,每个工厂需要1-5秒.
更新2:赏金已经结束,所以看起来没有解决方案.如果我找时间,我会写一个boost.parameter替换并在此处链接.使命名参数函数的返回类型依赖于输入类型以便更接近boost.hana样式语义也是很好的.
您可以通过全局 constexpr 静态对象实现,如下所示:
struct init_
{
my_class operator()(int i) {return i;}
my_class operator= (int i) {return i;}
};
consexpr static init_ init;
//make function
template<typename ...Args>
thingy make(Args&&...args);
auto x = make(init=42);
Run Code Online (Sandbox Code Playgroud)
您可能需要向 init_ 添加一个 constexpr ctor。然后,您可以使用 boost.fusion 或 boost.hana 获取序列的 for_each 并初始化您的类型。
struct thingy { int x;};
struct x_init
{
int init;
x_init (int i) : init(i) {};
void operator()(thingy & t)
{ t.x = init;}
};
struct x_
{
constexpr x_() {};
x_init operator()(int i) const {return i;}
x_init operator= (int i) const {return i;}
};
constexpr static x_ x;
template<typename ...Args>
thingy make(Args &&...)
{
thingy t;
auto vec = boost::fusion::make_vector(std::forward<Args>(args)...);
boost::fusion::for_each(vec, [t](auto & init){init(t);});
}
auto t = make(x=42);
Run Code Online (Sandbox Code Playgroud)
Boost.Process 实际上使用了它,如果你查看我在 github 上的存储库,你会发现它的一个相当复杂的版本。
归档时间: |
|
查看次数: |
344 次 |
最近记录: |