You need to enable JavaScript to run this app. 如何扩展C++参数解包工具,支持用std::vector参数调用类构造函数
优惠活动
大模型
产品
解决方案
定价
更多

如何扩展C++参数解包工具,支持用std::vector参数调用类构造函数

实现方案

核心需要扩展原有工具的类型萃取逻辑,新增针对类类型的构造函数参数推导能力,同时新增模板重载接口适配构造场景。假设目标类仅存在一个公开非模板构造函数(若有多个重载构造函数需额外指定参数列表,本方案适配最常用的单构造函数场景),具体修改如下:

完整可运行代码

#include <iostream>
#include <utility>
#include <vector>
#include <cstdint>
#include <memory>
#include <type_traits>

namespace util {
template <typename ReturnType, typename... Args>
struct function_traits_defs {
  static constexpr size_t arity = sizeof...(Args);

  using result_type = ReturnType;

  template <size_t i>
  struct arg {
    using type = typename std::tuple_element<i, std::tuple<Args...>>::type;
  };
};

template <typename T>
struct function_traits_impl;

template <typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(Args...)>
    : function_traits_defs<ReturnType, Args...> {};

template <typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(*)(Args...)>
    : function_traits_defs<ReturnType, Args...> {};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...)>
    : function_traits_defs<ReturnType, Args...> {};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) const>
    : function_traits_defs<ReturnType, Args...> {};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) const&>
    : function_traits_defs<ReturnType, Args...> {};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) const&&>
    : function_traits_defs<ReturnType, Args...> {};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) volatile>
    : function_traits_defs<ReturnType, Args...> {};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) volatile&>
    : function_traits_defs<ReturnType, Args...> {};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) volatile&&>
    : function_traits_defs<ReturnType, Args...> {};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) const volatile>
    : function_traits_defs<ReturnType, Args...> {};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) const volatile&>
    : function_traits_defs<ReturnType, Args...> {};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) const volatile&&>
    : function_traits_defs<ReturnType, Args...> {};

template <typename T, typename V = void>
struct function_traits
    : function_traits_impl<T> {};

template <typename T>
struct function_traits<T, decltype((void)&T::operator())>
    : function_traits_impl<decltype(&T::operator())> {};

template <size_t... Indices>
struct indices {
  using next = indices<Indices..., sizeof...(Indices)>;
};
template <size_t N>
struct build_indices {
  using type = typename build_indices<N - 1>::type::next;
};
template <>
struct build_indices<0> {
  using type = indices<>;
};
template <size_t N>
using BuildIndices = typename build_indices<N>::type;

// 新增:构造函数参数数量萃取,默认支持0-3个参数,需要更多参数可向下追加特化
template <typename T, typename = void>
struct construct_arity {};

template <typename T>
struct construct_arity<T, std::void_t<decltype(T())>> {
  static constexpr size_t value = 0;
};

template <typename T>
struct construct_arity<T, std::void_t<decltype(T(std::declval<int>()))>> {
  static constexpr size_t value = 1;
};

template <typename T>
struct construct_arity<T, std::void_t<decltype(T(std::declval<int>(), std::declval<int>()))>> {
  static constexpr size_t value = 2;
};

template <typename T>
struct construct_arity<T, std::void_t<decltype(T(std::declval<int>(), std::declval<int>(), std::declval<int>()))>> {
  static constexpr size_t value = 3;
};

namespace details {
template <typename FuncType,
          typename VecType,
          size_t... I,
          typename Traits = function_traits<FuncType>,
          typename ReturnT = typename Traits::result_type>
ReturnT do_call(FuncType& func, const VecType& args, indices<I...>) {
  return func(args[I]...);
}

// 新增:构造函数调用的内部实现
template <typename T, typename VecType, size_t... I>
T* do_construct(const VecType& args, indices<I...>) {
  return new T(args[I]...);
}
}  // namespace details

template <typename FuncType, typename VecType, typename Traits = function_traits<FuncType>>
auto unpack_caller(FuncType& func, const VecType& args) {
  return details::do_call(func, args, BuildIndices<Traits::arity>());
}

// 新增:针对类构造的重载接口,完全匹配需求的调用格式
template <typename T, typename VecType>
T* unpack_caller(const VecType& args) {
  constexpr size_t arity = construct_arity<T>::value;
  return details::do_construct<T>(args, BuildIndices<arity>());
}

// 可选扩展:返回智能指针避免内存泄漏
template <typename T, typename VecType>
std::unique_ptr<T> unpack_caller_unique(const VecType& args) {
  constexpr size_t arity = construct_arity<T>::value;
  return std::unique_ptr<T>(details::do_construct<T>(args, BuildIndices<arity>()));
}
}  // namespace util

// 原有测试用普通函数
int func(int a, int b, int c) {
  return a + b + c;
}

// 需求示例的自定义类
class foo
{
public:
    foo(uint32_t a, uint32_t b, uint32_t c){
        std::cout << "foo构造成功,参数:" << a << " " << b << " " << c << std::endl;
    };
};

int main() {
  // 原有功能正常运行
  std::vector<int> args = {1, 2, 3};
  int j = util::unpack_caller(func, args);
  std::cout << "普通函数调用结果:" << j << std::endl;

  // 需求示例的调用格式,完全匹配
  foo* f = util::unpack_caller<foo>(args);
  delete f;

  // 智能指针版本测试
  auto f_unique = util::unpack_caller_unique<foo>(args);

  return 0;
}

扩展说明

如果需要支持超过3个参数的构造函数,只需按照construct_arity的特化格式向下追加对应参数数量的特化分支即可;如果类存在多个重载构造函数,可新增模板参数让用户显式指定构造函数的参数列表,避免推导二义性。

内容的提问来源于stack exchange,提问作者Zephyr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 12:45:03