c++ - 具有更改返回类型的可变参数模板参数列表
问题描述
我正在尝试使用不断变化的返回类型制作模板化可变参数。如果参数的数量是 1,那么它应该返回一个指向唯一参数的指针,否则它应该返回一个参数指针的元组:
int var = 0;
A *ptr_A = foo<A>(var);
auto *[ptr_A, ptr_B] = foo<A, B>(var);
这是我到目前为止的代码
template<typename T>
T* AddComp(EntityId entityID)
{
componentPool<T>* pool = GetOrCreatePool<T>();
return pool->Create(entityID);
}
template <typename... Args>
decltype(auto) AddComponent(EntityId entityID)
{
if constexpr (sizeof...(Args) == 1)
{
return AddComp <Args>(entityID);
}
else
{
return std::tuple<decltype(AddComponent<Args>({}))... > {AddComponent<Args>(entityID)...};
}
}
但是我遇到了几个错误:
如果A *ptr_A = foo<A>(var);
VS 说他不能从 void 转换为 A*,这说明 decltype(auto) 以某种方式变为 void
并且在return AddComp <Args>(entityID);
VS 中说必须扩展 Args 参数包。即使它是一个参数,我是否仍然需要以某种方式扩展 Args?
谢谢!
编辑:
如被问及,我提供了一个最小的例子
using EntityId = size_t;
class A
{
public:
int a;
};
class B
{
public:
int b;
};
class componentsManager
{
public:
template<typename T>
T* AddComp(EntityId entityID)
{
return new T();
}
template <typename... Args>
decltype(auto) AddComponent(EntityId entityID)
{
if constexpr (sizeof...(Args) == 1)
{
return AddComp <Args>(entityID);
}
else
{
return std::tuple<decltype(AddComponent<Args>({}))... > {AddComponent<Args>(entityID)...};
}
}
};
componentsManager m_manager;
EntityId id;
A *ptr_A1 = m_manager.AddComponent <A>(id);
auto *[ptr_A2, ptr_B] = m_manager.AddComponent<A, B>(id);// This will only compile in c++17 i believe, i am using c++14
编辑2:
解决方案
直截了当
if constexpr (sizeof...(Args) == 1)
{
return AddComp<Args...>(entityID);
}
Here's a solution that works with C++14:
template <typename T>
decltype(auto) AddComponent(EntityId entityID)
{
return AddComp<T>(entityID);
}
template <typename... Args>
decltype(auto) AddComponent(
std::enable_if_t<(sizeof...(Args) > 1), EntityId> entityID)
{
return std::tuple<decltype(AddComponent<Args>({}))...>{
AddComponent<Args>(entityID)...};
}
推荐阅读
- python - 在运行时覆盖第三方类的__call__方法,同时维护其他方法
- azure-data-explorer - 日志分析工作簿中的 kusto 查询
- python - Keras 模型 load_weights 失败
- window - 什么是 hosts.ics 文件?
- python - 如何在模块代码中确定它是在 Jupiter 记事本中执行的?
- c - 尝试返回字符数组引用时出现“错误:类型冲突”
- java - android 中 Matrix.MTRANS_X 或 Matrix.MSCALE_X 枚举的和谐 OS 中的替代方案是什么?
- microsoft-graph-api - 使用 MS Graph API 按企业电话号码过滤用户失败
- vue.js - 解析表数据中的参数vuejs
- java - Gradle 插件仅支持 Kotlin Gradle 插件版本 1.3.40 及更高版本 将 android studio gradle 从 4.2.2 升级到 7.0.0 时出错