具有常量数组参数的模板 [英] Templates with constant array parameter

查看:55
本文介绍了具有常量数组参数的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如何通过分配的整数数组来参数化模板,

声明为static和constant,所以我可以编译时间

优化取决于数组的内容。我的方式

写了我的代码让g ++抱怨它不是静态的。


BTW我是C ++语言的新手,也是它提供的模板。


这是我想要做的一个例子


模板< bool条件,类然后,类Else>

struct IF {

typedef Then RET;

};


template< class Then,class Else>

struct IF< false,然后,Else> {

typedef Else RET;

};


struct A {

static void exec( ){

cout<< "是" <<结束;

}

};


struct B {

static void exec(){

cout<< [否" <<结束;

}

};


模板< const int mask []>

void foo(){

IF< mask [0] == 1,

A,

B> :: RET :: exec( );

}


struct运行{

static void run(){

static const int filter [] = {1,1,1};


foo< filter>();

}

} ;


int main(){

运行:: run();


返回0;

}


我希望编译的代码只包含


cout<<的说明"是" <<结束;


但正如我所说g ++抱怨并说


错误:非常数`((*& filter)== 1 )''不能用作模板

参数


错误来自声明


IF< mask [0] == 1,

A,

B> :: RET :: exec();


希望他们的有人可以告诉我我做错了什么或者是否可以使用数组进行编译时优化。


祝你好运,

Kristian Bisgaard Lassen

Hi,

How do I parameterize a template by a a allocated array of integers,
which is declared static and constant, so I can make compile time
optimizations depending on the content of the array. The way I have
written my code makes g++ complain about it not being static.

BTW I am new to the C++ language and also the templates it provides.

Here is an example of what I want to do

template<bool condition, class Then, class Else>
struct IF {
typedef Then RET;
};

template<class Then, class Else>
struct IF<false, Then, Else> {
typedef Else RET;
};

struct A {
static void exec () {
cout << "yes" << endl;
}
};

struct B {
static void exec () {
cout << "no" << endl;
}
};

template<const int mask[]>
void foo () {
IF<mask[0] == 1,
A,
B>::RET::exec ();
}

struct Run {
static void run () {
static const int filter[] = {1,1,1};

foo<filter>();
}
};

int main () {
Run::run();

return 0;
}

I want the compiled code just to contain the instructions for

cout << "yes" << endl;

But as I said g++ complains and says

error: non-constant `((*&filter) == 1)'' cannot be used as template
argument

The error is from the statement

IF<mask[0] == 1,
A,
B>::RET::exec ();

Hope their is someone who can tell me what I am doing wrong or if is
even possible make compiletime optimizations using arrays.

Best regards,
Kristian Bisgaard Lassen

推荐答案




我发现了那个int *上的修饰符const仅承诺

引用不会改变。它所指出的并不是一成不变的。你可以吗?
做一些declearation所以数组ints = {1,1,1}是不变的

也在内容级别? IE浏览器。 ints静态地是一个{1,1,1}的数组。

这基本上就是我需要让这个例子工作的。


一个替代方案是使用一对,每个

对的头部是一个常数整数。但是我真的想以某种方式使用数组

,因为指定数据集非常方便。


祝你好运,

Kristian Bisgaard Lassen

Hi,

I have found out that the modifier const on int* only promises that the
reference does not change. What it points to is not constant. Can you
make some sort of declearation so an array ints = {1,1,1} is constant
also on the content level? Ie. ints is statically an array of {1,1,1}.
That is basically what I need to make the example work.

An alternativ solution would be to use a pair, where the head of each
pair is an constant integer. But I would really like to use an array
somehow since it is a very convenient whay to specify a dataset.

Best regards,
Kristian Bisgaard Lassen





我发现int上的修饰符const *只承诺

参考不会改变。它所指出的并不是一成不变的。你可以吗?
做一些declearation所以数组ints = {1,1,1}是不变的

也在内容级别? IE浏览器。 ints静态地是一个{1,1,1}的数组。

这基本上就是我需要让这个例子工作的。


一个替代方案是使用一对,每个

对的头部是一个常数整数。但是我真的想以某种方式使用数组

,因为指定数据集非常方便。


祝你好运,

Kristian Bisgaard Lassen

Hi,

I have found out that the modifier const on int* only promises that the
reference does not change. What it points to is not constant. Can you
make some sort of declearation so an array ints = {1,1,1} is constant
also on the content level? Ie. ints is statically an array of {1,1,1}.
That is basically what I need to make the example work.

An alternativ solution would be to use a pair, where the head of each
pair is an constant integer. But I would really like to use an array
somehow since it is a very convenient whay to specify a dataset.

Best regards,
Kristian Bisgaard Lassen


Kristian Bisgaard Lassen写道:
Kristian Bisgaard Lassen wrote:
如何通过aa参数化模板分配的整数数组,
声明为静态和常量,因此我可以根据数组的内容进行编译时优化。我编写代码的方式让g ++抱怨它不是静态的。
How do I parameterize a template by a a allocated array of integers,
which is declared static and constant, so I can make compile time
optimizations depending on the content of the array. The way I have
written my code makes g++ complain about it not being static.




用作tempate参数的布尔表达式的值必须是

在编译时确定。即使有足够的信息可用

来确定值(如你发布的示例代码),编译器

可能无法这样做。很抱歉成为坏消息的承担者。 :(



The value of a boolean expression used as a tempate argument must be
determined at compile time. Even when enough information is available
to determine the value (as in the example code you posted), the compiler
may fail to do so. Sorry to be the bearer of bad news. :(


这篇关于具有常量数组参数的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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