• Advertisement
Sign in to follow this  

C++ pedantic, Implicit Declaration

This topic is 4640 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

True or False: An object if defined, need not be declared. New question: I can't figure this one out. Is there anything you can initialize this refernece to? int (*&r5)[5] = ?; [Edited by - Magmai Kai Holmlor on June 8, 2005 10:13:59 PM]

Share this post


Link to post
Share on other sites
Advertisement
I absolutely believe a definition also acts as a declaration. I don't have my C++ Standard here, unfortunately. Great, now I'll lose sleep over trying to find a counter-example.

Curse you, MKH.

Edit - a nameless struct type might be considered a borderline case: struct { int i; } foo;. It does declare and define a variable, also defines a type, but it could be argued that the type itself is not really declared... Idem for nameless temporaries. I don't know what the standard says though.

Share this post


Link to post
Share on other sites
False? You don't mention that it's defined before it's used, whereas declaration kind of implies that the declaration is before it's used.

Share this post


Link to post
Share on other sites
Good enough, what I really wanted to know is that the answer was too ambiguous to answer without a Standard witch-hunt.

I also thought it was an implicit declaration, so the question becomes does the definition count as a declaration thus the answer is false, or is it true because it's not declared elsewhere.

Share this post


Link to post
Share on other sites
false.

i think clause 3, section 1, para 2 indicates this (3.1 -2- )...

http://www.kuzbass.ru:8086/docs/isocpp/basic.html#basic.def

basically any definition is also a declaration. this is fine because there can be many declarations. for example the following is well-formed...

void foo ( int );
void foo ( int );
void foo ( int );
void foo ( int ) { }

Share this post


Link to post
Share on other sites
Quote:
Original post by Magmai Kai Holmlor
New question:
I can't figure this one out. Is there anything you can initialize this refernece to?
int (*&r5)[5] = ?;


GCC allows one to initialize it to itself...:

int (*&r5)[5] = r5;

Of course this will have undefined behavior and might not be ISO C++...

Share this post


Link to post
Share on other sites
Quote:
New question:
I can't figure this one out. Is there anything you can initialize this refernece to?
int (*&r5)[5] = ?;


Well, it is a reference to a pointer to an array of 5 int, so it should be well-formed to initialize it with a pointer to an array of 5 ints. The latter, in turn, can be initialized with an address of such an array:


int foo[5];
int (*bar)[5] = &foo; // note the address-of operator is required
int (*&baz)[5] = bar;


Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
Quote:
Original post by Magmai Kai Holmlor
New question:
I can't figure this one out. Is there anything you can initialize this refernece to?
int (*&r5)[5] = ?;


GCC allows one to initialize it to itself...:

int (*&r5)[5] = r5;

Of course this will have undefined behavior and might not be ISO C++...

the declarative region of a declarator in the above case extends from the directly after the point of declaration (and before initializers). this is the example given in the standard (3.3.1 -1-):

int x = 12;
{ int x = x; } // x is initialized with it's own (indetermiate) value

there are some exceptions or really they are gotchas:

const int x = 2;
{ int x[x]; } // array of 2 ints because x[] is not done being declared yet

or...

const int x = 12;
{ enum { x = x }; // x is initialized to 12 because the enum is still being declared


_ugly

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement