如何将参数列表存储到向量?
How to store arguments list to vector?
如何将可变构造函数参数存储到 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_;
};
- C++11
for(auto &&i: {args...}) keys.push_back(std::move(i));
- C++17
(keys.push_back(args), ...);
- 哦,抱歉,我错过了显而易见的事情:
template<class... Args> Combo(Args... args): keys_{uint8_t(args)...} {}
你可以这样写:
template <class... Args>
Combo(Args... args)
{
(keys_.push_back(args), ...);
}
在c++17 using 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<typename... Args>
Combo(Args &&... args): keys_ { std::forward<Args>(args)... } {}
甚至更好
Combo(std::initializer_list<uint8_t> keys): keys_(keys) {}
如何将可变构造函数参数存储到 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_;
};
- C++11
for(auto &&i: {args...}) keys.push_back(std::move(i));
- C++17
(keys.push_back(args), ...);
- 哦,抱歉,我错过了显而易见的事情:
template<class... Args> Combo(Args... args): keys_{uint8_t(args)...} {}
你可以这样写:
template <class... Args>
Combo(Args... args)
{
(keys_.push_back(args), ...);
}
在c++17 using 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<typename... Args>
Combo(Args &&... args): keys_ { std::forward<Args>(args)... } {}
甚至更好
Combo(std::initializer_list<uint8_t> keys): keys_(keys) {}