By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,300 Members | 1,369 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,300 IT Pros & Developers. It's quick & easy.

Self-referential initialization

P: n/a
Hi!

Is the following construct legal in C++?

int i = i;

It seems to be working on the compilers that I have. If it
indeed is legal, what is the rationale?

Note also that the following very dangerous construct, too,
seems to be working (probably for the same rationale):

int &r = r;

Cheers!

- Risto -

Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Thu, 13 Nov 2003 13:26:37 GMT, "Risto Lankinen"
<rl******@hotmail.com> wrote:
Hi!

Is the following construct legal in C++?

int i = i;
It is well-formed (it should compile), but executing it leads to
undefined behaviour, since you are initializing an int with an
indeterminate int value.
It seems to be working on the compilers that I have. If it
indeed is legal, what is the rationale?
The declaritive scope of a variable starts after its declarator but
before its initializer. I suppose it allows things like:

struct A
{
A(A* other);
};

A a = &a;

which might be useful to someone (although I'm not sure that it is
legal).

Note also that the following very dangerous construct, too,
seems to be working (probably for the same rationale):

int &r = r;


Again, well formed, but illegal.

Tom
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.