头文件中的const声明 [英] Const declaration in header files

查看:56
本文介绍了头文件中的const声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在我的C ++知识中,我只是去了一些灰色区域:


1)如果我有


const int SomeConst = 1;


在头文件中,它是全局的,它包含在多个翻译中

个单位,但它没有使用,我知道它不会占用

最终可执行文件中的存储空间。


以下内容


const double SomeConst2 = 2.0;

const char SomeConst3 [] =" A value1" ;;

const char * SomeConst4 =" A value 2" ;;


它们最终会在最终的可执行文件中结束吗?


2)最后两个语句缺少const并放在一个

头文件


char SomeConst5 [] ="值3" ;;

char * SomeConst6 =" A value 4;


此时,包含2个以上的翻译单元会违反一个

定义规则,对吧?


3)如果我有一个
const int SomeConst = 1;


在头文件中,它是全局的,是吗静态或外部默认?


谢谢


Stephen Howe

解决方案

" Stephen Howe" < sjhoweATdialDOTpipexDOTcomkirjutas:





我只知道C ++中的一些灰色区域:


1)如果我在头文件中有
const int SomeConst = 1;


,它是全局的,它包含在多个

翻译单元中,但它未被使用,我知道它不占用最终可执行文件中的
存储空间。



如果某些东西未使用,编译器可以将其扔出as-if基础,

或者不是。这主要是QOI问题。如果变量定义是不正确的,并且标准需要对此进行诊断,则

实现必须生成诊断,无论他是否b
会对可执行文件中的变量进行编码。


如果代码恰好取了某处的地址,那么编译器必须确保在b = b
运行时会有一个int变量,带有正确值的
,以便可以获取地址。这个最可靠的
可能意味着这个值以某种方式编码在可执行文件中 - 我想这就是你在
$ b中谈论存储时的意思$ b可执行文件。


但是,这一切都不是那么重要,除非您使用嵌入式

系统,计算单个字节。重要的是,在C ++中,const

对象定义可合法地出现在多个翻译单元中,而不会导致多个定义的链接器错误。当然,所有

定义必须相同。这样就可以在

公共头文件中使用这些定义,而不是古老的C宏。


>

以下怎么样


const double SomeConst2 = 2.0;

const char SomeConst3 [] =" A value1" ;;



与''const int''相同。这些定义可以包含在许多

翻译单元中。


const char * SomeConst4 =" A value 2" ;;



这里SomeConst4不是const,所以规则不适用,并且当试图将其包括在内时,一个将会发生链接器错误多个

翻译单位。添加常量以使其正常工作。


>

它们最终可执行文件中是否结束?



你为什么关心?


>

2)什么关于const丢失的最后两个语句,并在头文件中放置



char SomeConst5 [] ="值3;

char * SomeConst6 =" A value 4" ;;



你丢弃的const不是顶级的,所以它在这里没有任何

的意义。
< blockquote class =post_quotes>
>

此时,包含2个以上的翻译单元会违反

一个定义规则,对吗?



与SomeConst3(ok)和SomeConst4(失败)相同。


>

3)如果我在头文件中有
const int SomeConst = 1;


,它是全局的,默认是静态还是外部?



这不是正确的语言,标准是关于外部和

内部联系等等。我不确定我能得到详情如此我

跳过那个。


hth

Paavo


< blockquote> 2008年5月24日星期六19:22:55 +0100,Stephen Howe

< sjhoweATdialDOTpipexDOTcomwrote in comp.lang.c ++:





在我的C ++知识中,我只是浏览一些灰色区域:


1)如果我有


const int SomeConst = 1;


在头文件中,它是全局的,并且包含在多个翻译中

单位,但它没有使用,我知道它不占用

最终可执行文件中的存储空间。



它不是全局,C ++没有任何可以很好地映射

到这个概念通常意味着的东西。在C ++中使用的
命名空间作用域中使用的const关键字提供了内部链接,除非你

专门添加了extern关键字。


您定义的是具有内部链接的常量int。它可能会或可能不会占用最终可执行文件中的存储空间,如果你这样做,那么
就不会占用它的地址。如果你确实取了它的地址并以某种方式使用它,那么编译器无法验证它不会被解除引用,那么

它必须在运行时占用空间使用该地址的时间。


如果您将头文件包含在多个翻译单元中,则每个翻译单元将使用自己的不同副本。


下面的内容


const double SomeConst2 = 2.0;

const char SomeConst3 [ ] =" A value1";

const char * SomeConst4 =" A value 2";


它们最终会在最终的可执行文件中结束吗?



这完全取决于你的编译器,而不是语言,除非再次以某种方式使用这些对象的地址。


2)最后两个带有const缺失并放在

头文件中的语句怎么样


char SomeConst5 [] =" A value 3";

char * SomeConst6 =" A value 4";


此时,包括在2个以上的翻译单元中会违反一个

定义规则,对吗?



好​​吧,你几乎肯定会收到链接器的抱怨。


3)如果我有


const int SomeConst = 1;


在头文件中,它是全局的,默认情况下是静态还是extern?



不,它不是全球性的,C ++并没有真正具有全局性。最接近的

C ++必须是全局的外部链接。但是,在一个翻译单元中定义具有

外部链接的内容并不会自动地使其成为全局,可以在整个程序中通过名称访问。


命名空间范围内定义的任何对象都有静态存储持续时间。

如果定义包含const或static

关键字中的一个或两个,对象具有静态存储持续时间和内部链接。

如果定义中不包含这些关键字,则

对象具有静态存储持续时间和外部链接。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c+ + -faq-lite /

alt.comp.lang.learn.c-c ++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


2008年5月24日星期六15:46:58 -0500,Paavo Helde< no **** @ ebi.eewrote
$ b comp.lang.c ++中的$ b:


" Stephen Howe" < sjhoweATdialDOTpipexDOTcomkirjutas:





我只知道C ++中的一些灰色区域:


1)如果我在头文件中有
const int SomeConst = 1;


,它是全局的,它包含在多个

翻译单元中,但它未被使用,我知道它不占用最终可执行文件中的
存储空间。



[snip]


但是,这一切都不是那么重要,除非你在嵌入式

系统上工作,计算单个字节。重要的是,在C ++中,const

对象定义可合法地出现在多个翻译单元中,而不会导致多个定义的链接器错误。当然,所有

定义必须相同。这样就可以在

公共头文件中使用这些定义,而不是古老的C宏。



实际上,定义不一定完全相同,特别是对于

内置类型。


我可以在一个源文件中:


const int SomeConst = 1;


....而在另一个:


const int SomeConst = 42;


....完全没问题。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


Hi

Just going over some grey areas in my knowledge in C++:

1) If I have

const int SomeConst = 1;

in a header file, it is global, and it is included in multiple translations
units, but it is unused, I know that it does not take up storage in the
final executable.

What about the following

const double SomeConst2 = 2.0;
const char SomeConst3[] = "A value1";
const char *SomeConst4 = "A value 2";

do they wind up in the final executable?

2) What about the last two statements with const missing and placed in a
header file

char SomeConst5[] = "A value 3";
char *SomeConst6 = "A value 4";

At this point, including in 2+ translation units would fall foul of the one
definition rule, right?

3) If I have

const int SomeConst = 1;

in a header file, it is global, is it static or extern by default?

Thanks

Stephen Howe

解决方案

"Stephen Howe" <sjhoweATdialDOTpipexDOTcomkirjutas:

Hi

Just going over some grey areas in my knowledge in C++:

1) If I have

const int SomeConst = 1;

in a header file, it is global, and it is included in multiple
translations units, but it is unused, I know that it does not take up
storage in the final executable.

If something is unused, the compiler can throw it out on "as-if" basis,
or then not. This is mostly QOI issue. If the variable definitions are
incorrect and the standard requires a diagnostic about that, the
implementation has to produce the diagnostic regardless of whether he
would encode the variables in the executable or not.

If the code happens to take the address of i somewhere, the compiler is
obliged to ensure that at runtime there will be an int variable present
with the proper value, so that the address can be taken. This most
probably means that the value is somehow encoded in the executable - I
guess this is what you mean when you talk about storage in the
executable.

However, this is all not so important, unless you work on embedded
systems, counting single bytes. What is important that in C++ the const
object definitions can legally appear in multiple translation units,
without causing linker errors about multiple definitions. Of course, all
definitions must be identical. This enables to use such definitions in
common header files instead of ancient C macros.

>
What about the following

const double SomeConst2 = 2.0;
const char SomeConst3[] = "A value1";

The same as ''const int''. These definitions can be included in many
translation units.

const char *SomeConst4 = "A value 2";

Here SomeConst4 is not const, so the rule does not apply and one will
most probably get linker errors when trying to include this in multiple
translation units. Add ''const'' to make it working.

>
do they wind up in the final executable?

Why do you care?

>
2) What about the last two statements with const missing and placed
in a header file

char SomeConst5[] = "A value 3";
char *SomeConst6 = "A value 4";

The const you dropped is not the top-level one, so it does not have any
significance here.

>
At this point, including in 2+ translation units would fall foul of
the one definition rule, right?

The same as for SomeConst3 (ok) and SomeConst4 (fails).

>
3) If I have

const int SomeConst = 1;

in a header file, it is global, is it static or extern by default?

This is not the right termonology, the standard speaks about external and
internal linkage, etc. I am not sure I can get the details right so I
skip that.

hth
Paavo


On Sat, 24 May 2008 19:22:55 +0100, "Stephen Howe"
<sjhoweATdialDOTpipexDOTcomwrote in comp.lang.c++:

Hi

Just going over some grey areas in my knowledge in C++:

1) If I have

const int SomeConst = 1;

in a header file, it is global, and it is included in multiple translations
units, but it is unused, I know that it does not take up storage in the
final executable.

It is not "global", C++ does not have anything that maps all that well
to what is normally meant by that concept. The const keyword used at
namespace scope in C++ provides internal linkage, unless you
specifically add the extern keyword as well.

What you have defined is a constant int with internal linkage. It
might or might not take up storage in the final executable, if you do
not take its address. If you do take its address and use it in a way
such that compiler cannot verify that it won''t be dereferenced, then
it must take up space at run time when that address is used.

If you include the header file in multiple translation units, each
translation unit will wind up with its own distinct copy.

What about the following

const double SomeConst2 = 2.0;
const char SomeConst3[] = "A value1";
const char *SomeConst4 = "A value 2";

do they wind up in the final executable?

That is entirely up to your compiler, not the language, unless again
you use the address of these objects in some way.

2) What about the last two statements with const missing and placed in a
header file

char SomeConst5[] = "A value 3";
char *SomeConst6 = "A value 4";

At this point, including in 2+ translation units would fall foul of the one
definition rule, right?

Well, you would almost certainly have complaints from the linker.

3) If I have

const int SomeConst = 1;

in a header file, it is global, is it static or extern by default?

No, it isn''t global, C++ doesn''t really have "global". The closest
C++ has to global is external linkage. But defining something with
external linkage in one translation unit does not automagically make
it "global", that is accessible by name throughout the entire program.

Any objects defined at namespace scope have static storage duration.
If the definition includes either or both of the const or static
keywords, the object has static storage duration and internal linkage.
If the definition does not include either of these keywords, the
object has static storage duration and external linkage.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


On Sat, 24 May 2008 15:46:58 -0500, Paavo Helde <no****@ebi.eewrote
in comp.lang.c++:

"Stephen Howe" <sjhoweATdialDOTpipexDOTcomkirjutas:

Hi

Just going over some grey areas in my knowledge in C++:

1) If I have

const int SomeConst = 1;

in a header file, it is global, and it is included in multiple
translations units, but it is unused, I know that it does not take up
storage in the final executable.

[snip]

However, this is all not so important, unless you work on embedded
systems, counting single bytes. What is important that in C++ the const
object definitions can legally appear in multiple translation units,
without causing linker errors about multiple definitions. Of course, all
definitions must be identical. This enables to use such definitions in
common header files instead of ancient C macros.

Actually, the definitions need not be identical at all, especially for
built-in types.

I can have in one source file:

const int SomeConst = 1;

....and in another:

const int SomeConst = 42;

....with no problem at all.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


这篇关于头文件中的const声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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