小编Roe*_*man的帖子

我可以根据模板参数在堆栈上分配一系列变量吗?

在我正在编写的一段代码中,我收到了数据包uint8_t *std::size_t组合。我可以根据从哪个文件描述符接收数据包来注册要使用这两个参数调用的函数。我使用 anstd::map<int, std::function<void(const uint8_t *, std::size_t)> > handlers来跟踪要调用哪个函数。

我希望能够(间接)使用任意参数注册函数。uint8_t *我已经有一个这样的函数来从和转换std::size_t为单独的变量:

int unpack(const uint8_t *buf, std::size_t len) { return 0; }

template <typename T, typename... Types>
int unpack(const uint8_t *buf, std::size_t len, T &var1, Types... var2) {
  static_assert(std::is_trivially_copyable<T>::value, "unpack() only works for primitive types");
  if (len < sizeof(T)) return -1;
  var1 = *reinterpret_cast<const T *>(buf);
  const auto sum = unpack(buf + sizeof(T), len - sizeof(T), var2...);
  const auto ret = …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-templates c++20

6
推荐指数
1
解决办法
184
查看次数

标签 统计

c++ ×1

c++20 ×1

variadic-templates ×1