如何将参数列表存储到向量?

问题描述

如何将可变参数的构造函数参数存储到vector?

尝试失败的示例:

class Combo 
{
public:
   template <class... Args>
   Combo(Args... args) 
   {
      // this->keys_.push_back(args...);

      // this->keys_.push_back(args)...;

      // this->keys_.push_back(std::forward<Args>(args...));

      //for (uint8_t arg : args...)
      //  this->keys_.push_back(arg);

      // ???
   }

private:
   std::vector<uint8_t> keys_;
};

解决方法

  1. C ++ 11
for(auto &&i: {args...}) keys.push_back(std::move(i));
  1. C ++ 17
(keys.push_back(args),...);
  1. 哦,对不起,我错过了显而易见的事情:
template<class... Args> Combo(Args... args): keys_{uint8_t(args)...} {}
,

中使用fold expression,您可能会

#include <vector>
#include <utility> // std::forward

class Combo
{
public:
   template <class... Args>
   Combo(Args&&... args) 
   {
      keys_.reserve(sizeof...(Args));  // reserve memory for unwanted reallocation
      (keys_.emplace_back(std::forward<Args>(args)),...);
   }

private:
   std::vector<uint8_t> keys_;
};

但是,这将允许传递除uint8_t之外的其他类型,并且对于那些可以隐式转换为uint8_t的类型,将进行隐式转换。

这不是期望的行为。因此,我建议如下static_assert

#include <type_traits> // std::is_same_v

template <class... Args>
Combo(Args&&... args)
{
   // to make sure that the args all are of type `uint8_t`
   static_assert((std::is_same_v<uint8_t,Args> && ...),"Args should be uint8_t");

   keys_.reserve(sizeof...(Args));  // reserve some memory for unwanted reallocation
   (keys_.emplace_back(std::forward<Args>(args)),...);
}

这将为您提供以下错误

Combo obj{ 1,2,3,4.f };
//                  ^^^^ --> float
,

您可以写:

template <class... Args>
Combo(Args... args)
{
    (keys_.push_back(args),...);
}
,
template<typename... Args>
Combo(Args &&... args): keys_ { std::forward<Args>(args)... } {}

甚至更好

Combo(std::initializer_list<uint8_t> keys): keys_(keys) {}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...