仅在标头实现中的大型静态数组

时间:2013-06-26 19:29:51

标签: c++ arrays templates static const

由于需要能够在仅头部实现中引用unicode字符组,我想首先声明所有数组。模板允许我初始化标题中的静态成员,但代码看起来很乱。

这只是二十个组中的两个:

struct CharacterClass 
{
    struct Value
    {
        int l;
        int h;
    };
};

template < int N >
struct Nd : public CharacterClass
{
    static const Value v[];
};

template< int N >
const typename Nd< N >::Value Nd< N >::v[] = { 
    { 0x00030 ,  0x00039 }, { 0x00660 ,  0x00669 }, { 0x006f0 ,  0x006f9 }, { 0x007c0 ,  0x007c9 }, { 0x00966 ,  0x0096f },
    { 0x009e6 ,  0x009ef }, { 0x00a66 ,  0x00a6f }, { 0x00ae6 ,  0x00aef }, { 0x00b66 ,  0x00b6f }, { 0x00be6 ,  0x00bef },
    { 0x00c66 ,  0x00c6f }, { 0x00ce6 ,  0x00cef }, { 0x00d66 ,  0x00d6f }, { 0x00e50 ,  0x00e59 }, { 0x00ed0 ,  0x00ed9 },
    { 0x00f20 ,  0x00f29 }, { 0x01040 ,  0x01049 }, { 0x017e0 ,  0x017e9 }, { 0x01810 ,  0x01819 },     { 0x01946 ,  0x0194f },
    { 0x019d0 ,  0x019d9 }, { 0x01b50 ,  0x01b59 }, { 0x0ff10 ,  0x0ff19 }
};

template < int N >
struct Nl : public CharacterClass
{
    static const Value v[];
};
template< int N >
const typename Nl< N >::Value Nl< N >::v[] = { 
    { 0x016ee ,  0x016f0 },     { 0x02160 ,  0x02182 },  { 0x03007, 0x03007 },  { 0x03021 ,  0x03029},  { 0x03038 ,  0x0303a }
};

Q1:是否可以在基类中声明一次数组,而不必为每个派生类型重复一次?

Q2:我如何隐藏虚拟&#39; int N&#39;,这样我以后可以引用结构而不必添加模板参数?即。 int x = Nd.v [10] .l;

问题3:有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

我会尝试给你一个替代你问过的Q1。 我不喜欢看到对程序逻辑毫无意义的硬编码值。

这是我有时使用的一个很好的技巧:

创建一个新文件并将值粘贴到其中。

<强> data.dat文件

{ 0x00030 ,  0x00039 }, { 0x00660 ,  0x00669 }, { 0x006f0 ,  0x006f9 }, { 0x007c0 ,  0x007c9 }, { 0x00966 ,  0x0096f },
{ 0x009e6 ,  0x009ef }, { 0x00a66 ,  0x00a6f }, { 0x00ae6 ,  0x00aef }, { 0x00b66 ,  0x00b6f }, { 0x00be6 ,  0x00bef },
{ 0x00c66 ,  0x00c6f }, { 0x00ce6 ,  0x00cef }, { 0x00d66 ,  0x00d6f }, { 0x00e50 ,  0x00e59 }, { 0x00ed0 ,  0x00ed9 },
{ 0x00f20 ,  0x00f29 }, { 0x01040 ,  0x01049 }, { 0x017e0 ,  0x017e9 }, { 0x01810 ,  0x01819 },     { 0x01946 ,  0x0194f },
{ 0x019d0 ,  0x019d9 }, { 0x01b50 ,  0x01b59 }, { 0x0ff10 ,  0x0ff19 }

现在,当您想重复它时,请执行以下操作:

const typename Nd< N >::Value Nd< N >::v[] = { 
#include "data.dat"
};

这样,预处理器会为您粘贴所有这些值,因此您无需一直重复这些值。

答案 1 :(得分:0)

回答我自己的问题。我将整个组合数组放在一个基类中,然后从派生类型派生出来,将自己初始化为数组中的索引。

struct CharacterClass 
{
    struct Value
    {
        int l;
        int h;
    };
};

template < int X >
struct Base : public CharacterClass
{
    static const Value v[];
};
template< int X >
const typename Base< X >::Value Base< X >::v[] = { 
    // Nd values length 23 index 0
    { 0x00030 ,  0x00039 }, { 0x00660 ,  0x00669 }, { 0x006f0 ,  0x006f9 }, { 0x007c0 ,  0x007c9 }, { 0x00966 ,  0x0096f },
    { 0x009e6 ,  0x009ef }, { 0x00a66 ,  0x00a6f }, { 0x00ae6 ,  0x00aef }, { 0x00b66 ,  0x00b6f }, { 0x00be6 ,  0x00bef },
    { 0x00c66 ,  0x00c6f }, { 0x00ce6 ,  0x00cef }, { 0x00d66 ,  0x00d6f }, { 0x00e50 ,  0x00e59 }, { 0x00ed0 ,  0x00ed9 },
    { 0x00f20 ,  0x00f29 }, { 0x01040 ,  0x01049 }, { 0x017e0 ,  0x017e9 }, { 0x01810 ,  0x01819 },     { 0x01946 ,  0x0194f },
    { 0x019d0 ,  0x019d9 }, { 0x01b50 ,  0x01b59 }, { 0x0ff10 ,  0x0ff19 },

    // Nl values length 5 index 23
    { 0x016ee ,  0x016f0 }, { 0x02160 ,  0x02182 },  { 0x03007, 0x03007 },  { 0x03021 ,  0x03029},  { 0x03038 ,  0x0303a }
};

template < int _index, int _length >
struct BaseIndex : public Base< 0 >
{
    static const Value * v;
    static const int length;
};
template < int _index, int _length >
const typename BaseIndex< _index, _length >::Value * BaseIndex< _index, _length >::v = &Base::v[ _index ];
template < int _index, int _length >
const int BaseIndex< _index, _length >::length = _length;

struct Nd : public BaseIndex< 0, 23 >
{
};
struct Nl : public BaseIndex< 23, 5 >
{
};

我认为命名空间中的typedef只能替换那些struct的Nd和Nl。

如果有更好的方法,我仍然愿意接受建议。