简体   繁体   English

如何在不知道模板数据类型的情况下定义 static 模板变量?

[英]How can I define a static templated variable without knowing the template data type?

Let's say I have the following struct to store a reference to a class member variable:假设我有以下结构来存储对 class 成员变量的引用:

template <typename  R>
struct Foo {
    R ref;
    int info;
};
class Bar {
public:
    void* run();
};

I want to create a const variable of type foo that immediately sets the ref param as follows:我想创建一个 foo 类型的 const 变量,它立即设置 ref 参数如下:

const Foo theVar {&Bar::run, 0x1000}; //Doesn't work, but preferred
const Foo<void (Bar::*)()> theVar {&Bar::run, 0x1000}; //Does work, but rather not

The reason I'm using a template in this case is because there's no way to cast a class member variable to a void* , so I'm forced into this position. However, it seems that I cannot declare the variable without first telling the compiler what type I'm planning to use.我在这种情况下使用模板的原因是因为没有办法将 class 成员变量转换为void* ,所以我被迫进入这个 position。但是,似乎我不能在没有先告诉编译器我打算使用什么类型。 Although I do have this information, it's not the prettiest way to accomplish what I want and could possibly cause some issues in the long run.虽然我确实有这些信息,但这不是完成我想要的事情的最好方法,从长远来看可能会导致一些问题。

The compiler should be able to tell that I'm passing a variable of type void (Bar::*)() to this struct, so I'm almost certain there has to be a way around this issue.编译器应该能够告诉我正在将类型为void (Bar::*)()的变量传递给此结构,因此我几乎可以肯定必须有解决此问题的方法。

The reason I'm in need of this struct is because I need a reference to exist for the linker. I don't want it to be set on run-time, it needs to be available for the linker. Using templates seems to be the only way to accomplish this.我需要这个结构的原因是因为我需要 linker 的引用。我不希望它在运行时设置,它需要对 linker 可用。使用模板似乎是实现这一目标的唯一途径。

The solution is simple: add a deduction guide after Foo declaration:解决方法很简单:在Foo声明后添加推导指南

template<typename R>
struct Foo {
    R ref;
    int info;
};

template<typename R> Foo(R, int) -> Foo<R>;

Demo演示

暂无
暂无

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

相关问题 在不知道底层类型的情况下,如何从模板化父类取回值? - How can I get the value back from a templated parent class without knowing the underlying type? 如何在不知道类型的情况下声明模板指针? - How can I declare a template pointer without knowing the type? 是否可以在不知道模板类型的情况下指向模板化类? - Is it possible to point to templated class without knowing the template type? 如何在模板化类型上专门化模板类的静态成员? - How do I specialize a static member of a template class on a templated type? 如何在不知道该类是否为模板的情况下调用模板化的成员函数? - How do I call a templated member function without knowing whether or not the class is a template? 如何声明/定义/初始化模板类的静态成员变量为类的静态成员变量? - How can I Declare/define/initialize a static member variable of template classes as static member variables of a class? 如何为模板化类型专门化/重载模板函数 - How can I specialize/overload a template function for a templated type 可以在没有模板参数的情况下使用模板化类的静态类函数吗? - Can static class functions of a templated class be used without a template parameter? 如何定义模板的模板化子类? - How to define a templated subclass of a template? 如何为模板化类的成员函数定义静态数组? - How do I define a static array to member functions of a templated class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM