英文:
How can I define a static templated variable without knowing the template data type?
问题
以下是您要翻译的内容:
让我们假设我有以下结构体来存储对类成员变量的引用:
```cpp
template <typename R>
struct Foo {
R ref;
int info;
};
class Bar {
public:
void* run();
};
我想创建一个类型为Foo
的常量变量,立即将ref
参数设置如下:
const Foo theVar {&Bar::run, 0x1000}; //不起作用,但更可取
const Foo<void (Bar::*)()> theVar {&Bar::run, 0x1000}; //起作用,但不太理想
在这种情况下我使用模板的原因是因为没有办法将类成员变量转换为void*
,所以我被迫采用这种方式。然而,似乎我不能在告诉编译器我计划使用的类型之前声明变量。虽然我有这个信息,但这不是实现我想要的方式,可能会在长期内引发一些问题。
编译器应该能够识别我正在向这个结构体传递类型为void (Bar::*)()
的变量,所以我几乎可以确定一定有方法解决这个问题。
我需要这个结构体的原因是因为我需要一个引用供链接器使用。我不希望它在运行时设置,它需要在链接器中可用。使用模板似乎是唯一的解决方法。
<details>
<summary>英文:</summary>
Let's say I have the following struct to store a reference to a class member variable:
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:
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. 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.
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.
</details>
# 答案1
**得分**: 3
解决方法很简单:在`Foo`声明后添加一个[推导指南](https://en.cppreference.com/w/cpp/language/class_template_argument_deduction#Deduction_for_class_templates):
```cpp
template<typename R>
struct Foo {
R ref;
int info;
};
template<typename R> Foo(R, int) -> Foo<R>;
英文:
The solution is simple: add a deduction guide after Foo
declaration:
template<typename R>
struct Foo {
R ref;
int info;
};
template<typename R> Foo(R, int) -> Foo<R>;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论