473,624 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question on incomplete definitions

ark
Hello group,
Could you help me with this:

static const int x;
............ something .............
static const int x = 17;

It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the first line,
"warning C4132: 'x' : const object should be initialized"
yet generates correct code.

What is correct - the code or the compiler? If the code, is it known what
compilers choke on this and how hard?

Thanks,
Ark
Nov 14 '05 #1
24 2090

"ark" <ar****@comcast .net> wrote in message
static const int x;
This is a tentative forward declaration of x.
........... something .............
static const int x = 17;
This is the declaration of x
It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the first
line, "warning C4132: 'x' : const object should be initialized"
yet generates correct code.

What is correct - the code or the compiler? If the code, is it known
what compilers choke on this and how hard?

The compiler is allowed to give warnings for code which is correct but looks
suspicious. Here you are using a little-used construct with a const, so it
wonders if you know what you are doing.

I would also give a warning, since I suspect that you don't need a tentative
forward declaration at all of a static const integer in your code. However I
may be wrong.
Nov 14 '05 #2
ark <ar****@comcast .net> wrote:
Hello group,
Could you help me with this: static const int x;
You declare and define x, with the implicit value of 0.
However, since the object is const, you won't have a chance
to change its value. AFAIK, this construct is valid, but
is largely meaningless.
........... something .............
static const int x = 17;
You redeclare and redefine x with the value of 17. This
is obviously incorrect because the x identifier already
exists and has storage.
It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the first line,
"warning C4132: 'x' : const object should be initialized"
yet generates correct code.


This is a friendly warning that says that your first line
doesn't make much sense. It is probably not a required
diagnostic.

Alex
Nov 14 '05 #3
ark

"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote in message
news:br******** **@newsg1.svr.p ol.co.uk...
<snip> I would also give a warning, since I suspect that you don't need a tentative forward declaration at all of a static const integer in your code. However I may be wrong.


Imagine, e.g., a header file that #define's useful thing about x (which is
really likely to be a struct of some sort). Then the header needs a
"tentative forward declaration" of x, and here it is useful IMHO.
Sorry for wrong terminology,
- Ark
Nov 14 '05 #4
ark

"Alex" <al*******@hotm ail.com> wrote in message
news:br******** ****@ID-190529.news.uni-berlin.de...
ark <ar****@comcast .net> wrote:
Hello group,
Could you help me with this:

static const int x;


You declare and define x, with the implicit value of 0.
However, since the object is const, you won't have a chance
to change its value. AFAIK, this construct is valid, but
is largely meaningless.
........... something .............
static const int x = 17;


You redeclare and redefine x with the value of 17. This
is obviously incorrect because the x identifier already
exists and has storage.
It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the first line, "warning C4132: 'x' : const object should be initialized"
yet generates correct code.


This is a friendly warning that says that your first line
doesn't make much sense. It is probably not a required
diagnostic.

Alex


I am not sure your analysis is correct; see Malcolm's posting above.
- Ark
Nov 14 '05 #5
On Wed, 17 Dec 2003 22:01:25 -0000, "Malcolm"
<ma*****@55bank .freeserve.co.u k> wrote:
static const int x;

This is a tentative forward declaration of x.

How does the compiler distinguish this from a normal declaration with
default initialization?

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #6
Alan Balmer <al******@att.n et> writes:
On Wed, 17 Dec 2003 22:01:25 -0000, "Malcolm"
<ma*****@55bank .freeserve.co.u k> wrote:
static const int x;

This is a tentative forward declaration of x.

How does the compiler distinguish this from a normal declaration with
default initialization?


See C99 6.9.2#2:

2 A declaration of an identifier for an object that has file
scope without an initializer, and without a storage-class
specifier or with the storage-class specifier static,
constitutes a tentative definition. If a translation unit
contains one or more tentative definitions for an
identifier, and the translation unit contains no external
definition for that identifier, then the behavior is exactly
as if the translation unit contains a file scope declaration
of that identifier, with the composite type as of the end of
the translation unit, with an initializer equal to 0.

--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I'll interact with on any given
day."
--Billy Chambless
Nov 14 '05 #7
ark

"Ben Pfaff" <bl*@cs.stanfor d.edu> wrote in message
news:87******** ****@pfaff.stan ford.edu...
Alan Balmer <al******@att.n et> writes:
<snip> See C99 6.9.2#2:

2 A declaration of an identifier for an object that has file
scope without an initializer, and without a storage-class
specifier or with the storage-class specifier static,
constitutes a tentative definition. If a translation unit
contains one or more tentative definitions for an
identifier, and the translation unit contains no external
definition for that identifier, then the behavior is exactly
as if the translation unit contains a file scope declaration
of that identifier, with the composite type as of the end of
the translation unit, with an initializer equal to 0.

--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I'll interact with on any given
day."
--Billy Chambless


So... I can do it only in C99, not in classic C (90?) or whatever was
before?
What about K&R?
Thanks for all help,
- Ark
Nov 14 '05 #8
ark wrote:

"Alex" <al*******@hotm ail.com> wrote in message
news:br******** ****@ID-190529.news.uni-berlin.de...
ark <ar****@comcast .net> wrote:
Hello group,
Could you help me with this:

static const int x;


You declare and define x, with the implicit value of 0.
However, since the object is const, you won't have a chance
to change its value. AFAIK, this construct is valid, but
is largely meaningless.
........... something .............
static const int x = 17;


You redeclare and redefine x with the value of 17. This
is obviously incorrect because the x identifier already
exists and has storage.
It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the first line, "warning C4132: 'x' : const object should be initialized"
yet generates correct code.


This is a friendly warning that says that your first line
doesn't make much sense. It is probably not a required
diagnostic.

Alex


I am not sure your analysis is correct; see Malcolm's posting above.
- Ark

Alex may be more correct that Malcolm. The whole point of the const
qualifier is that its object should not be modified without diagnostic.
You noticed that..

const int x;

yielding a diagnostic because you missed your only 'legal' chance to
define the value of x. It is not clear that the diagnostic is required
but x clearly useless in this case. Further down in your code..

const int x = 17;

redifines x and assigns a value to it. The redifinition is the error
here and requires a diagnostic. Assigning 17 to x here is probably ok.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #9
"ark" <ar****@comcast .net> writes:
"Ben Pfaff" <bl*@cs.stanfor d.edu> wrote in message
news:87******** ****@pfaff.stan ford.edu...
Alan Balmer <al******@att.n et> writes:
<snip>
See C99 6.9.2#2:

2 A declaration of an identifier for an object that has file
scope without an initializer, and without a storage-class
specifier or with the storage-class specifier static,
constitutes a tentative definition. If a translation unit
contains one or more tentative definitions for an
identifier, and the translation unit contains no external
definition for that identifier, then the behavior is exactly
as if the translation unit contains a file scope declaration
of that identifier, with the composite type as of the end of
the translation unit, with an initializer equal to 0.


So... I can do it only in C99, not in classic C (90?) or whatever was
before?


There is similar text in C90 if I recall correctly. I generally
quote from C99 because I have an accurate electronic
transcription.
What about K&R? Thanks for all help, - Ark


K&R didn't have the concept of a tentative definition as far as I
know.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
33564
by: Lou Pecora | last post by:
g++ compiler error question. I have a container C whose constructor takes a class B that is inherited from an abstract class A. So I have the line of code: B binstance; C cinstance(binstance); The compiler gives the error,
5
3726
by: Paul F. Dietz | last post by:
Is the following legal C? struct foo; struct foo (*p); /* Pointer to array of 10 foo structures */ struct foo { int bar; int baz; }; main() { printf("%d\n", sizeof(*p)); } Paul Dietz dietz@dls.net
3
11794
by: John Sasso | last post by:
In my Yacc .y file I defined: %union { int value; struct Symbol Sym; } The Symbol struct I defined in a header file I #included in the Prologue section of the .y file as:
5
2079
by: friend.05 | last post by:
1) #include <stdio.h> #include <stdlib.h> #include "graph.h" #define MAX_VTX 4 struct _graph_vertex {
8
3846
by: friend.05 | last post by:
I have three files. graph.h: My header file where I have typedef for my structure and function delcaration typedef struct _ipc_actors ipc_graph_actors; typedef ipc_graph_type *ipc_graph_pointer;
2
6183
by: Kai-Uwe Bux | last post by:
Does the following have undefined behavior? struct X; struct Y { X * x_ptr; Y ( void ) : x_ptr ( 0 )
3
2056
by: massysett | last post by:
Greetings, Having classes with member objects that have STL containers of objects whose definitions are incomplete results in undefined behavior. See for example: http://www.ddj.com/database/184403814#8 http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.14 I am wondering: is it okay to have member functions that return an STL
5
1963
by: fmassei | last post by:
Hello, I'm have a question about forward structure declarations and incomplete types. The complete structure of my file is pretty complex, but just as an example the code below will explain the problem: struct b; struct a { struct b m; }; struct b {
50
4466
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
8234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8620
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.