c++ - 使用模板参数推导推导类的 "outer class"

标签 c++

class Component_Base{};

class Rect_Component : Component_Base
{
public:
    struct Info
    {
        uInt width;
        uInt height;
    };
};

template<typename... Args>
class ECSData
{
private:
    tuple<optional_ECSVector_wrapped_t<Args>...> data;

public:

    template<typename... Info>
    void createEntity(Info&&... info) {
    }
};

int main()
{
    ECSData<Rect_Component, Text_Component, Rendering_System> ecsData;
    ecsData.createEntity(Rect_Component::Info{ .width{200}, .height{500} });
    return 0;
}

在上面的代码中,我将 Rect_Component::Info 传递给 createEntity()。 鉴于上面的代码,可以在 createEntity() 内部让模板参数推导推断出 T::Info 中的 T 是什么,并使用说T? 毕竟我在调用中编写了 Rect_Component,所以我怀疑一定有办法

最佳答案

考虑:

struct Not_Rect_Component {
    using Info = Rect_Component::Info;
};

这无法与您的原始 Rect_Component 提供的 Info 区分开来,因此您无法推断其中之一。您必须在 Info 中提供一个 typedef,它指的是正确的父类型。

关于c++ - 使用模板参数推导推导类的 "outer class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59429222/

相关文章:

c++ - 如何让 C++ 程序监听系统命令

c# - 局域网连接开启或关闭

c++ - CryptGenRandom 和 CNG BCryptGenRandom API 之间的区别

C++ 命名空间声明和定义

c++ - WM_COPYDATA 数组与 vector

c++ - 质因数分解

c++ - 如何在 C++ 中使用制作/使用 Doxygen 过滤器?

c++ - 编写可与多个版本的 Tcl 一起使用的 Tcl 扩展

c++ - C/C++ 相当于 Bash "readlink -f"

c++ - 为什么不推荐SQLite C接口(interface)中的接口(interface)sqlite3_get_table