如何使用一个静态数组元素的索引对象的静态数组是不同的模板实例 [英] How do I use a static array element as an index to a static array of objects that are of different template instantiations

查看:75
本文介绍了如何使用一个静态数组元素的索引对象的静态数组是不同的模板实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用一个静态数组元素的索引对象的静态数组是不同的模板实例?

How do I use a static array element as an index to a static array of objects that are of different template instantiations?

我卡越来越明显的编译器错误:对'N'无效的模板参数,预计编译时间常数前pression

I'm stuck getting the obvious compiler error: invalid template argument for 'N', expected compile-time constant expression.

我不知道,如果有一个非C ++ 11的回答。希望有现代的东西,我可以在vs2013使用...:)

I don't know if there's a non-c++11 answer. Hopefully there's something modern that I can use in vs2013... :)

我想存储数据静态像这样:

I'm trying to store data statically like so:

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;
}

我要访问对象的静态数组中,但我不能使用非编译常数做到这一点。我跑VS2013更新4。

I want access to the objects in the static array but I can't do it with a non-compile constant. I'm running VS2013 Update 4.

推荐答案

有关使用 constexpr 某些原因,而不是常量下面一行对我的作品。我不知道是什么原因呢。见<一href=\"http://stackoverflow.com/questions/30534332/using-an-element-of-a-constexpr-array-vs-a-const-array-to-instantiate-a-template\">a问题我问关于这个问题的。

For some reason using constexpr instead of const in the following line works for me. I don't know why yet. See a question I asked on the subject.

// static const size_t ARRAYSIZES[2] =   // Does not work
static constexpr size_t ARRAYSIZES[2] =  // Works
{
    1,
    2
};

下面statemenent将创建两个子对象,但将仅存储在父对象,它是空的。我不知道怎么这将帮助你。

The following statemenent will create two child objects but will store only the parent object, which is empty. I don't know how that will help you.

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

也许你需要的指针。

Perhaps you need pointers.

static DataParent* DataTable[ 2 ] =
{
    new DataChild< 1 >(&size1Array),
    new DataChild< 2 >(&size2Array)            
};

或智能指针。

static std::unique_ptr<DataParent> DataTable[ 2 ] =
{
    new DataChild< 1 >(&size1Array),
    new DataChild< 2 >(&size2Array)            
};

更新

读多一点关于 constexpr ,我明白了,为什么之后

After reading a bit more about constexpr, I understand why

static constexpr size_t ARRAYSIZES[2] =  { ... };

的作品,但

static const size_t ARRAYSIZES[2] =  { ... };

不起作用。

A constexpr 需要在编译的时候可求而常量不需要

A constexpr needs to be evaluatable at compile time while a const need not.

由于

double read_double()
{
   double v;
   std::cin >> v;
   return v;
}

const double v1 = read_double();

就可以了。 V1 在运行时初始化。初始化完成后,它保持不变。

is ok. v1 is initialized at run time. Once initialized, it remains constant.

constexpr double v2 = read_double();

是不是因为 OK read_double()不是 constexpr ,并且不能在编译时进行评估。因此,它不能被用来初始化 V1 在编译时。

is not OK since read_double() is not a constexpr and cannot be evaluated at compile time. Hence, it cannot be used to initialize v1 at compile time.

这篇关于如何使用一个静态数组元素的索引对象的静态数组是不同的模板实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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