简体   繁体   English

'尝试解析可变参数模板时无法推断出'T'的模板参数

[英]'Could not deduce template argument for 'T'' when trying to parse variadic template

I'm trying to pass in a list of struct types into another class, which will take each custom class and add sort of put the struct type into a wrapper and insert that into a tuple.我正在尝试将结构类型列表传递给另一个 class,它将采用每个自定义 class 并将结构类型添加到包装器中并将其插入到元组中。 I'm new to templates in general, and can't figure out why the code wont compile.一般来说,我是模板的新手,无法弄清楚为什么代码无法编译。

The template-parser-wrapper thing模板解析器包装器

namespace engine
{
    template<class T>
    struct component_manager
    {
        int component_id;

        std::vector<T> components;

        component_manager() : component_id(id_counter) { id_counter++; }
    };

    template<class... Ts>
    class ecs_manager
    {
    public:
        std::tuple<> components;
        
        template<class... Ts>
        ecs_manager()
        {
            constructor_helper<Ts...>();
        }

        template<class T, class... Ts>
        void constructor_helper()
        {
            components = std::tuple_cat(components, component_manager<T>());
            constructor_helper<Ts...>();
        }

        template<class T>
        void constructor_helper() {}
    };
}

The structs结构

struct transform
{
    engine::vector3 position;
    engine::vector3 rotation;
    engine::vector3 scale;
};

struct motion
{
    engine::vector3 velocity;
};

struct mesh
{
    int id;
};

Creating the template-parser-wrapper-thing engine::ecs_manager<transform, motion, mesh> ecs;创建 template-parser-wrapper-thing engine::ecs_manager<transform, motion, mesh> ecs;

When compiling, I get these: Could not deduce template argument for ' T ' No matching overloaded function found编译时,我得到这些: 无法推断' T '的模板参数没有找到匹配的重载 function

Not sure... but I suppose you're looking for不确定......但我想你正在寻找

template <typename ... Ts>
class ecs_manager
 {
   public:
      std::tuple<component_manager<Ts>...> components;
    
      ecs_manager () 
          : components{ component_manager<Ts>{} ... }
       { }
 };

Anyway... C++ is a strongly typed language.无论如何... C++ 是一种强类型语言。

So you can't define an empty tuple所以你不能定义一个空元组

std::tuple<> components;

and recursively increment it并递归地递增它

components = std::tuple_cat(components, component_manager<T>());

You have to define components as you want and you can't change it's type run-time您必须根据需要定义components ,并且不能更改它的类型运行时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM