Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 返回类型变化的可变模板参数列表_C++_Templates_C++14_Variadic - Fatal编程技术网

C++ 返回类型变化的可变模板参数列表

C++ 返回类型变化的可变模板参数列表,c++,templates,c++14,variadic,C++,Templates,C++14,Variadic,我正试图使一个模板可变返回类型。 如果参数数为1,则应返回指向唯一参数的指针,否则应返回参数指针的元组: int var = 0; A *ptr_A = foo<A>(var); auto *[ptr_A, ptr_B] = foo<A, B>(var); int-var=0; A*ptr_A=foo直截了当的 if constexpr (sizeof...(Args) == 1) { return AddComp<Args...>(entityID

我正试图使一个模板可变返回类型。 如果参数数为1,则应返回指向唯一参数的指针,否则应返回参数指针的元组:

int var = 0;
A *ptr_A = foo<A>(var);
auto *[ptr_A, ptr_B] = foo<A, B>(var);
int-var=0;
A*ptr_A=foo

直截了当的

if constexpr (sizeof...(Args) == 1)
{
    return AddComp<Args...>(entityID);
}
如果constexpr(sizeof…(Args)==1)
{
返回AddComp


下面是一个使用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)...};
}
模板
decltype(自动)AddComponent(EntityId EntityId)
{
返回AddComp(entityID);
}
模板
decltype(自动)添加组件(
std::启用(如果为1),EntityId>EntityId)
{

return std::tupleYou need
return AddComp(entityID);
@HolyBlackCat是的,我以前试图添加扩展名
,但我收到一个错误,说“预期a>”。你能做一个吗?@cigien我添加了一个我认为有用的示例。你说你在使用C++14,但你始终依赖C++17的功能,例如,
if constexpr
。你能不能使用C++17?在C++17之前,你需要两个重载。工作非常完美。由于错误的转发,IntelliSense错误地抛出了该错误。
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)...};
}