Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11711 > unrolled thread

extern const reference

Started by"Igor R." <igor.rubinov@gmail.com>
First post2011-11-22 11:47 -0800
Last post2011-11-22 20:53 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  extern const reference "Igor R." <igor.rubinov@gmail.com> - 2011-11-22 11:47 -0800
    Re: extern const reference "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-22 21:22 +0100
    Re: extern const reference jt@toerring.de (Jens Thoms Toerring) - 2011-11-22 20:53 +0000

#11711 — extern const reference

From"Igor R." <igor.rubinov@gmail.com>
Date2011-11-22 11:47 -0800
Subjectextern const reference
Message-ID<65256071-1753-479b-a9ea-39079aded9c1@j10g2000vbe.googlegroups.com>
Hello,

I've got the following code:

int main()
{
	const extern int &ref;
	{
		int var = 5;
		const int &ref = var;
	}
	int var = ref;
}

I.e., I'd like to define and initialize a const ref in a local scope,
but want to *declare* and to be able to use it in the outer scope. I
attempt to declare it as extern, but this doesn't link well, i.e.
"const int &ref = var;" isn't interpreted as the above extern
definition, but as another local variable.
Is it possible to do the above trick somehow? (I know I can
restructure the code as the last resort.)

Thanks.

[toc] | [next] | [standalone]


#11713

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2011-11-22 21:22 +0100
Message-ID<jah0a9$12f$1@dont-email.me>
In reply to#11711
On 22.11.2011 20:47, Igor R. wrote:
> Hello,
>
> I've got the following code:
>
> int main()
> {
> 	const extern int&ref;
> 	{
> 		int var = 5;
> 		const int&ref = var;
> 	}
> 	int var = ref;
> }
>
> I.e., I'd like to define and initialize a const ref in a local scope,
> but want to *declare* and to be able to use it in the outer scope. I
> attempt to declare it as extern, but this doesn't link well, i.e.
> "const int&ref = var;" isn't interpreted as the above extern
> definition, but as another local variable.
> Is it possible to do the above trick somehow? (I know I can
> restructure the code as the last resort.)

For your given example you can just do

   int main()
   {
       int const ref = 5;
       int var = ref;
   }

If that doesn't solve your problem, then it may be because I failed to 
understand what the original problem was.

This sounds suspiciously like a case of trying to solve problem X by 
applying technique Y, then failing to make Y work, and asking about Y. 
Don't ask about technique Y. Ask about the original problem X.

Cheers & hth.,

- Alf

[toc] | [prev] | [next] | [standalone]


#11721

Fromjt@toerring.de (Jens Thoms Toerring)
Date2011-11-22 20:53 +0000
Message-ID<9j2gdgFfvmU1@mid.uni-berlin.de>
In reply to#11711
Igor R. <igor.rubinov@gmail.com> wrote:
> I've got the following code:

> int main()
> {
>         const extern int &ref;
>         {
>                 int var = 5;
>                 const int &ref = var;
>         }
>         int var = ref;
> }

> I.e., I'd like to define and initialize a const ref in a local scope,
> but want to *declare* and to be able to use it in the outer scope. I
> attempt to declare it as extern, but this doesn't link well, i.e.
> "const int &ref = var;" isn't interpreted as the above extern
> definition, but as another local variable.
> Is it possible to do the above trick somehow? (I know I can
> restructure the code as the last resort.)

If you could do that in the sense you seem to want it to
work the last line would involve undefined behaviour since
you then would be using use a reference to a variable that
already has gone out of scope. What you seem to try to get
to work looks very similar to me to e.g.

int const & get_var( )
{
     int v = 5;
	 return v;
}

int main( )
{
    int const & ref = get_var( );
    int var = ref;
}

And that, of course, doesn't work since the moment the
get_var() function is left nothing remains of its local
variable 'v', so holding on to a reference to it doesn't
make sense.

What is what you're trying to do meant to be good for? Per-
haps there's something that can be done but it's not really
clear to me what you want to achieve.

                            Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web