在模板实例化之间共享静态成员? (不可能?) [英] Sharing static members between template instantiations? (impossible?)

查看:193
本文介绍了在模板实例化之间共享静态成员? (不可能?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图以某种方式专门化类型,我需要我自己的查找结构,它本质上是全局的(但是最好封装为类变量),但是我希望对象是类型安全的,所以它们被参数化。



,基本上

  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 的各种实例化。



我怀疑它可能只是一个管道梦,正确的答案只是使它成为一个单独的全局单例对象,但是无论如何,对于所有 SpecialArray只有一个 lookupTable s?



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

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

...但可惜的是GCC不能在我心中编译C ++



有没有什么实际的方法来获得我想要的(在C ++ 0x-land或某处的某个地方)?我可能遇到这个问题,也有一些静态方法操纵这个查找表(不跟踪类型或Ns)。



...对不起if



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

解决方案

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

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

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


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.

解决方案

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
{
    //...
};

这篇关于在模板实例化之间共享静态成员? (不可能?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆