在编译时已知 [英] Known at compile-time

查看:80
本文介绍了在编译时已知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下用G ++为我编译:

const unsigned int z(5U);


unsigned int y(5 + 6 - 4/2 + 3 * 6 - z);


inline unsigned int x()

{

返回y * 4 + z - 9 * 3;

}


inline unsigned int w()

{

unsigned int temp((x()> 8?x()+ 5:x() - 4));


temp - = 4;


temp * = 5;


返回temp + 6;

}


unsigned int v( unsigned int输入)

{

if(输入> 16)

{

返回输入+ w( );

}

其他

{

返回输入 - x();

}

}


#include< iostream>

使用std :: cout;

使用std :: endl;


int main()

{

char a [z];

$ b $ + = 4;


char b [y];


char c [x()] ;


char d [w()];


char e [v(w())];


cout<< sizeof(a)<< endl

<< sizeof(b)<< endl

<< sizeof(c)<< endl

<< sizeof(d)<< endl

<< sizeof(e);

}

我想知道是否可以保证在每个C ++标准符合
的编译器上编译。这些值确实是可知的。在编译时,但它似乎在我看来编译器有一些工作要做。

它为我打印以下内容:

5

26

82

421

842


其他人想试一试吗?

-JKop

解决方案



" JKop" < NU ** @ NULL.NULL>在留言中写道

新闻:6z ***************** @ news.indigo.ie ...

以下用G ++为我编译:

const unsigned int z(5U);

unsigned int y(5 + 6 - 4/2 + 3 * 6 - z);

内联unsigned int x()
{
返回y * 4 + z - 9 * 3;
}
内联unsigned int w ()
{
unsigned int temp((x()> 8?x()+ 5:x() - 4));

temp - = 4;

temp * = 5;

返回temp + 6;
}

unsigned int v(unsigned int input)
{
if(输入> 16)
{
返回输入+ w();
}

返回输入 - x();
}

#include< iostream>
使用std :: cout;
使用std :: endl;

int main()
{char a [z];

y + = 4;

char b [ y];

char c [x()];

char d [w()];

char e [v(w())];

cout<< sizeof(a)<< endl
<< sizeof(b)<< endl
<< sizeof(c)<< endl
<< sizeof(d)<< endl
<< sizeof(e);
}

我想知道是否可以保证在每个符合C ++标准的编译器上编译。




这些数值确实是可知的。在编译时,但在我看来,编译器有一些工作要做。
它为我打印以下内容:




g ++有一个扩展,其中数组边界不必为常量。尝试使用-ansi开关编译



我忘了编译时常量的规则是什么,但它确实是

不包括函数调用。简单的积分表达式和sizeof都可以,

指针算术也可以。


john


< blockquote> JKop写道:

以下用G ++为我编译:

const unsigned int z(5U);

unsigned int y(5 + 6 - 4/2 + 3 * 6 - z);

内联unsigned int x()
{
返回y * 4 + z - 9 * 3;
}

内联unsigned int w()
{
unsigned int temp((x()> 8?x()+ 5:x( ) - 4));

temp - = 4;

temp * = 5;

返回temp + 6;
}

unsigned int v(unsigned int input)
{
if(input> 16)
{
返回输入+ w();
}

{
返回输入 - x();
}
}
#include< iostream>
使用std :: cout;
使用std :: endl;

int main()
{
char a [z];

y + = 4;

char b [y];

char c [x()];

char d [w()];

char e [v(w())];

cout<< sizeof(a)<< endl
<< sizeof(b)<< endl
<< sizeof(c)<< endl
<< sizeof(d)<< endl
<< sizeof(e);
}

我想知道是否可以保证在每个符合C ++标准的编译器上编译。这些值确实是可知的。在编译时,


但这并不能使它成为编译时常量。

但在我看来,编译器有一点工作它将为我打印以下内容:

5
26
82
421
842
>其他人想尝试一下吗?




MSVC6.0呛到它。


你可以逃脱它在G ++上,因为它支持可变长度的
数组。在其他时候你这样做:


char b [y];


y不一定是编译时常量。


下面的代码也可能在G ++上编译(我没有在我现在使用的系统上安装G ++

所以我不能我自己测试),甚至

虽然编译器无法事先知道你将会是什么。


#include< iostream>

使用std :: cout;

使用std :: cin;

使用std :: endl;


int main()

{

int y;

std :: cin>> y;

char b [y];

cout<< sizeof(b)<< endl;


返回0;

}

但是可变长度数组不是标准C ++(但它是标准C99)。 br />

-

Peter van Merkerk

peter.van.merkerk(at)dse.nl

John Harrison发布:

g ++有一个扩展,其中数组边界不需要
常量。尝试使用-ansi开关进行编译。



它仍然使用-ansi编译。

关闭以进行更多实验...

-JKop


The following compiles for me with G++:
const unsigned int z(5U);

unsigned int y(5 + 6 - 4 / 2 + 3 * 6 - z);

inline unsigned int x()
{
return y * 4 + z - 9 * 3;
}

inline unsigned int w()
{
unsigned int temp( ( x() > 8 ? x() + 5 : x() - 4 ) );

temp -= 4;

temp *= 5;

return temp + 6;
}

unsigned int v(unsigned int input)
{
if ( input > 16 )
{
return input + w();
}
else
{
return input - x();
}
}

#include <iostream>
using std::cout;
using std::endl;

int main()
{
char a[z];

y += 4;

char b[y];

char c[x()];

char d[w()];

char e[v(w())];

cout << sizeof(a) << endl
<< sizeof(b) << endl
<< sizeof(c) << endl
<< sizeof(d) << endl
<< sizeof(e);
}
I''m wondering if this is guaranteed to compile on every C++ Standard
compliant compiler. The values are indeed "knowable" at compile time, but it
seems to me that a compiler has a bit of work to do.
It prints the following for me:

5
26
82
421
842

Anyone else want to give it a try?
-JKop

解决方案


"JKop" <NU**@NULL.NULL> wrote in message
news:6z*****************@news.indigo.ie...

The following compiles for me with G++:
const unsigned int z(5U);

unsigned int y(5 + 6 - 4 / 2 + 3 * 6 - z);

inline unsigned int x()
{
return y * 4 + z - 9 * 3;
}

inline unsigned int w()
{
unsigned int temp( ( x() > 8 ? x() + 5 : x() - 4 ) );

temp -= 4;

temp *= 5;

return temp + 6;
}

unsigned int v(unsigned int input)
{
if ( input > 16 )
{
return input + w();
}
else
{
return input - x();
}
}

#include <iostream>
using std::cout;
using std::endl;

int main()
{
char a[z];

y += 4;

char b[y];

char c[x()];

char d[w()];

char e[v(w())];

cout << sizeof(a) << endl
<< sizeof(b) << endl
<< sizeof(c) << endl
<< sizeof(d) << endl
<< sizeof(e);
}
I''m wondering if this is guaranteed to compile on every C++ Standard
compliant compiler.
No
The values are indeed "knowable" at compile time, but it
seems to me that a compiler has a bit of work to do.
It prints the following for me:



g++ has an extension where array bounds do NOT have to constant. Try
compiling with the -ansi switch.

I forget what the rules for compile time constants are exactly, but it does
not include function calls. Simple integral expressions and sizeof are OK,
pointer arithmetic is probably OK as well.

john


JKop wrote:

The following compiles for me with G++:
const unsigned int z(5U);

unsigned int y(5 + 6 - 4 / 2 + 3 * 6 - z);

inline unsigned int x()
{
return y * 4 + z - 9 * 3;
}

inline unsigned int w()
{
unsigned int temp( ( x() > 8 ? x() + 5 : x() - 4 ) );

temp -= 4;

temp *= 5;

return temp + 6;
}

unsigned int v(unsigned int input)
{
if ( input > 16 )
{
return input + w();
}
else
{
return input - x();
}
}

#include <iostream>
using std::cout;
using std::endl;

int main()
{
char a[z];

y += 4;

char b[y];

char c[x()];

char d[w()];

char e[v(w())];

cout << sizeof(a) << endl
<< sizeof(b) << endl
<< sizeof(c) << endl
<< sizeof(d) << endl
<< sizeof(e);
}
I''m wondering if this is guaranteed to compile on every C++ Standard
compliant compiler. The values are indeed "knowable" at compile time,
But that doesn''t make it a compile time constant.
but it seems to me that a compiler has a bit of work to do.
It prints the following for me:

5
26
82
421
842

Anyone else want to give it a try?



MSVC6.0 chokes on it.

You can get away with it on G++ because it supports variable length
arrays. In other other when you do:

char b[y];

y doesn''t have to be a compile time constant.

The code below will probably compile on G++ as well (I haven''t got G++
installed on the system I''m using now so I cannot test it myself), even
though the compiler has no way of knowing in advance what y will be.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
int y;
std::cin >> y;
char b[y];
cout << sizeof(b) << endl;

return 0;
}
However variable length arrays are not standard C++ (but it standard C99).

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


John Harrison posted:

g++ has an extension where array bounds do NOT have to constant. Try compiling with the -ansi switch.


It still compiles with -ansi.
Off to do more experiments...
-JKop


这篇关于在编译时已知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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