C++ 如何使用静态数组元素作为不同模板实例化的静态对象数组的索引

C++ 如何使用静态数组元素作为不同模板实例化的静态对象数组的索引,c++,arrays,c++11,C++,Arrays,C++11,如何使用静态数组元素作为不同模板实例化的静态对象数组的索引 我遇到了一个明显的编译器错误:“N”的模板参数无效,应该是编译时常量表达式 我不知道是否有非c++11的答案。希望我可以在vs2013中使用一些现代的东西…:) 我尝试静态存储数据,如下所示: static const char* size1Array[1] = { "hello" }; static const char* size2Array[2] = { "foo", "bar" }; static c

如何使用静态数组元素作为不同模板实例化的静态对象数组的索引

我遇到了一个明显的编译器错误:“N”的模板参数无效,应该是编译时常量表达式

我不知道是否有非c++11的答案。希望我可以在vs2013中使用一些现代的东西…:)

我尝试静态存储数据,如下所示:

static const char* size1Array[1] =
{
    "hello"
};

static const char* size2Array[2] =
{
    "foo",
    "bar"
};

static const size_t ARRAYSIZES[2] =
{
    1,
    2
};

// Empty parent
struct DataParent {};

template <size_t N>
struct DataChild : DataParent
{
    DataChild(const char*(*arrIn)[N])
        : arr(arrIn) {}

    const char*(*arr)[N];
};

// The arrays are of different sizes, hence the DataParent to keep them in a static array
static DataParent DataTable[ 2 ] =
{
    DataChild< 1 >(&size1Array),
    DataChild< 2 >(&size2Array)            
};

int main()
{
    int index = 1;
    // The tricky cast that won't compile (ARRAYSIZES[index] 
    std::cout << ((DataChild< ARRAYSIZES[index] >*)&DataTable[index])->arr[0] << std::endl;
}
static const char*size1Array[1]=
{
“你好”
};
静态常量字符*size2Array[2]=
{
“福”,
“酒吧”
};
静态常量大小数组化[2]=
{
1.
2.
};
//空父级
结构DataParent{};
模板
结构DataChild:DataParent
{
DataChild(常量字符*(*arrIn)[N])
:arr(arrIn){}
常量字符*(*arr)[N];
};
//数组的大小不同,因此DataParent需要将它们保存在一个静态数组中
静态数据父数据表[2]=
{
DataChild<1>(&size1Array),
DataChild<2>(&size2Array)
};
int main()
{
int指数=1;
//无法编译的复杂强制转换(数组化[索引]

std::cout*)&DataTable[index])->arr[0]出于某种原因,在下一行中使用
constepr
而不是
const
对我有效。我还不知道为什么。请参阅主题

// static const size_t ARRAYSIZES[2] =   // Does not work
static constexpr size_t ARRAYSIZES[2] =  // Works
{
    1,
    2
};
下面的语句将创建两个子对象,但只存储父对象,它是空的。我不知道这将如何帮助您

static DataParent DataTable[ 2 ] =
{
    DataChild< 1 >(&size1Array),
    DataChild< 2 >(&size2Array)            
};
工作但是

static const size_t ARRAYSIZES[2] =  { ... };
不起作用

constepr
需要在编译时可计算,而
const
则不需要

给定

正常。
v1
在运行时初始化。一旦初始化,它将保持不变

constexpr double v2 = read_double();

不正常,因为
read\u double()
不是
constexpr
,无法在编译时进行计算。因此,它不能用于在编译时初始化
v1

您有几个问题:

  • 将DataParent存储为值时的切片问题
所以

以下不是编译时常量:

ARRAYSIZES[index] // Not a compile time value.

为什么数组的大小必须是一个模板参数?为什么
DataChild
不能只是一个存储
const char**
的普通类?我想要有指向静态const char*[]的数据。但是,考虑到你的问题,我最终决定在DataChild中使用一个向量,而不是const char*(*arr)使用initializer-lists.Oops初始化不同大小的数组。抱歉,我输入了一个错误。我更新了代码以反映DataChild继承自DataParent。因此,是的,DataParent结构是空的,但是DataChild对象将具有数据并且是父类型的。@starpax,我假设是这样。我建议使用指针或智能指针仍然有效。我可能在这里遗漏了一些东西,在没有对DataChild*或DataChild的模板实例化进行正确转换的情况下,如何从DataParent*或unique_ptr访问数组?我认为免费存储中的分配无法解决需要编译时指定转换的问题。(不幸的是,我不能在vs2013中使用constexpr)。哦,我看到你关于指针的观点是指上面提到的Jarod42切片问题。谢谢你的评论!
static const size_t ARRAYSIZES[2] =  { ... };
double read_double()
{
   double v;
   std::cin >> v;
   return v;
}

const double v1 = read_double();
constexpr double v2 = read_double();
static DataParent DataTable[2] =
{
    DataChild<1>(&size1Array), // Slicing
    DataChild<2>(&size2Array)  // Slicing
};
static DataChild<1> child1(&size1Array);
static DataChild<2> child2(&size2Array);

static DataParent* DataTable[2] = {&child1, &child2};
const size_t ARRAYSIZES[2] = { 1, 2 };
int index = 1;
ARRAYSIZES[index] // Not a compile time value.