使用auto / decltype推导出数组类型

时间:2016-03-24 10:40:18

标签: c++11 template-meta-programming

要创建std ::对的字符串和绑定函数,我已设法执行以下操作

auto f1 = std::make_pair(std::wstring(L"send_order"),std::bind(&RESTListener::send_order, this, std::placeholders::_1));
auto f2 = std::make_pair(std::wstring(L"on_cancel_order"), std::bind(&RESTListener::on_cancel_all, this, std::placeholders::_1));
auto connectors = std::array<decltype(f1),2>({ f1,f2 });

有没有办法自动推断出数组的类型,所以我可以按照

的方式输入内容
auto connectors = std::array<?????,2>({std::make_pair(std::wstring(L"send_order"),std::bind(&RESTListener::send_order, this, std::placeholders::_1)),std::make_pair(std::wstring(L"on_cancel_order"), std::bind(&RESTListener::on_cancel_all,this, std::placeholders::_1))});

1 个答案:

答案 0 :(得分:1)

正如@nwp所指出的,更简单的解决方案是记住在执行auto foo = {,}时创建了std :: initializer_list。所以更简洁的解决方案是

auto connectors = {std::make_pair(std::wstring(L"send_order"),std::bind(&RESTListener::send_order, this, std::placeholders::_1)),
                   std::make_pair(std::wstring(L"on_cancel_order"), std::bind(&RESTListener::on_cancel_all, this, std::placeholders::_1)) };