在模板实例之间共享静态成员? (不可能的?)

发布于 2024-09-08 17:12:39 字数 938 浏览 6 评论 0原文

我正在做一些可能很愚蠢的事情,但如果它有效的话那就太好了。

我试图以某种方式专门化类型,我需要自己的查找结构,该结构本质上是全局的(但理想情况下封装为类变量),但我希望对象是类型安全的,因此它们是参数化的。

因此,本质上

template<class T, int N>
class SpecialArray
{
//...
private:
   static map<string, internal_t> lookupTable
}

,无论出于何种原因,我直到初始化 LookupTable 时才想到 当我说

template <class T, int N>
SpecialArray<T,N>::lookupTable;

将会有许多不同的 lookupTable 运行在附加到 SpecialArray 的各种实例上。

我怀疑这可能只是一个白日梦,正确的答案只是使其成为一个单独的全局单例对象,但是无论如何都可以使其只有一个 lookupTable 用于所有 >特殊数组

就像,在我心目中的 C++ 中(不是真正的 C++),这会是这样的

template <class T, int N>
SpecialArray<*,*>::lookupTable;

……但遗憾的是 GCC 没有编译我心目中的 C++

有没有任何实际的方法可以得到我想要的东西(在 C+ 中的某个地方) +0x-土地什么的)?我也可能会遇到这个问题,使用一些操作这个查找表的静态方法(它不跟踪类型或 Ns)。

...抱歉,如果这没有任何意义。

预先感谢您提供的任何帮助或同情。

I am doing something that is probably silly, but it would be nice if it worked.

I am attempting to specialize types in a way that I need my own lookup structure that is essentially global (but ideally encapsulated as a class variable), but I want the objects to be type safe, so they are parameterized.

Consequently, I have, essentially

template<class T, int N>
class SpecialArray
{
//...
private:
   static map<string, internal_t> lookupTable
}

and for whatever reason, I didn't think until such time as I went to initialize lookupTable
that when I say

template <class T, int N>
SpecialArray<T,N>::lookupTable;

there are going to be many different lookupTables running around attached to the various instantiations of SpecialArray.

I suspect it may just be a pipe dream and that the correct answer is just making it a separate global singleton object, but is there anyway to make it such that there is just one lookupTable for all the SpecialArrays?

Like, in the C++ in my mind (which is not real C++), this would go something like

template <class T, int N>
SpecialArray<*,*>::lookupTable;

... but sadly GCC does not compile the C++ in my mind

Is there any actual way to get what I want (somewhere in C++0x-land or something)? I am likely to run into this problem also with some static methods that manipulate this lookup table (which does not keep track of types or Ns).

... sorry if that didn't make any sense.

Thanks in advance for any help or sympathy you can render.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2024-09-15 17:12:39

您可以添加一个非模板化基类并将 lookupTable 移动到该类中:

class Base
{
protected:
    static map<string, internal_t> lookupTable
};

template<class T, int N>
class SpecialArray : Base
{
    //...
};

You could add a non-templated base class and move lookupTable into that class:

class Base
{
protected:
    static map<string, internal_t> lookupTable
};

template<class T, int N>
class SpecialArray : Base
{
    //...
};
萌逼全场 2024-09-15 17:12:39

听起来你应该要么让它们全球化,而不用担心它。
也许使用命名空间就是您想要的?

Sounds like you should either just make them global and not worry about it.
Perhaps using namespaces is what you want?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文