Reassignment to a const variable that was declared in an if()

O

Oplec

Hi,

I thought that I understood how C++ allows for the declaration and
defining of variables within an if() statement and how the declared
variable can be used until the end of the major if() block.

if (int a = /*...*/) {
// ...
}
else if (a = /*variable a be assigned*/) {
/* variable a can still be used here */
}
else {
/* variable a can still be used here */
}
/* variable a can not be used here */

In the process of coding I used a const variable in the if statement and
then assigned to the const variable in the "else if" part, by mistake.
However, the GCC compiler that I am using compiled the code without any
warnings or errors returned. I read in TC++PL that a variable declared
const can only be initialized and never assigned. And that is why I have
written this post. Is GCC in error, or my understanding of standard C++?

if (const int a = /*...*/) {
// ...
}
else if (a = /*can a be assigned here???*/) {
/* Can the variable be assigned to above and then used here with the
new value? */
}

Thanks, oplec.
 
B

Buster

Oplec said:
Hi,

I thought that I understood how C++ allows for the declaration and
defining of variables within an if() statement and how the declared
variable can be used until the end of the major if() block.

if (int a = /*...*/) {
// ...
}
else if (a = /*variable a be assigned*/) {
/* variable a can still be used here */
}
else {
/* variable a can still be used here */
}
/* variable a can not be used here */

That's right.
In the process of coding I used a const variable in the if statement and
then assigned to the const variable in the "else if" part, by mistake.
However, the GCC compiler that I am using compiled the code without any
warnings or errors returned. I read in TC++PL that a variable declared
const can only be initialized and never assigned. And that is why I have
written this post. Is GCC in error, or my understanding of standard C++?

No, you're correct.
if (const int a = /*...*/) {
// ...
}
else if (a = /*can a be assigned here???*/) {
/* Can the variable be assigned to above and then used here with the
new value? */
}

Thanks, oplec.

I get

Owner@MACHINE ~/projects/scratch
$ cat if-scope.cpp
#include <cstdlib>
int main ()
{
if (const int a = std::rand () / (RAND_MAX / 4)) { }
else if (a = std::rand () / (RAND_MAX / 4)) { }
}

Owner@MACHINE ~/projects/scratch
$ g++ -o if-scope.exe if-scope.cpp
if-scope.cpp: In function `int main()':
if-scope.cpp:5: assignment of read-only variable `a'

Owner@MACHINE ~/projects/scratch
$ g++ --version
g++.exe (GCC) 3.2 (mingw special 20020817-1)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Oplec escribió:
if (const int a = /*...*/) {
// ...
}
else if (a = /*can a be assigned here???*/) {
/* Can the variable be assigned to above and then used here with the
new value? */
}

I tested that:

int main ()
{
if (const int a= 0)
{
}
else if (a= 1)
{
}
}

In gcc 3.3 and the result is:

if.cpp: In function `int main()':
if.cpp:9: error: assignment of read-only variable `a'

Regards.
 
O

Oplec

Oplec said:
Hi,

I thought that I understood how C++ allows for the declaration and
defining of variables within an if() statement and how the declared
variable can be used until the end of the major if() block.

I thought the code provided in my original post was an example of what
was compiling in my code, but it turns out it wasn't. I took apart my
program to code a function that compiles and reassigns to a const char*
and confirmed that it compiles with my current GCC. I took out all the
parts that didn't affect the irregularity and so the function isn't
practical, but in my code there is a recursive find function for a
binary tree. I took out all that stuff and what is below is what
compiles without error, but reassigns to a const. If you play around
with it, you will notice that if const char* b = 0 is used, it works,
but if const char* b = 7 is used, an error results in that line.

const char* find_helper()
{
if (const char* b = find_helper()){
return b;
}
else if (b = find_helper()) {
return b;
}
}

So, please see if this code snippet compiles.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Oplec escribió:
const char* find_helper()
{
if (const char* b = find_helper()){
return b;
}
else if (b = find_helper()) {
return b;
}
}

Here b is not const, is a pointer to const char, but the pointer itself
is not const.

Regards.
 
N

Neil Gan

Oplec said:
I thought the code provided in my original post was an example of what
was compiling in my code, but it turns out it wasn't. I took apart my
program to code a function that compiles and reassigns to a const char*
and confirmed that it compiles with my current GCC. I took out all the
parts that didn't affect the irregularity and so the function isn't
practical, but in my code there is a recursive find function for a
binary tree. I took out all that stuff and what is below is what
compiles without error, but reassigns to a const. If you play around
with it, you will notice that if const char* b = 0 is used, it works,
but if const char* b = 7 is used, an error results in that line.

const char* find_helper()
{
if (const char* b = find_helper()){
return b;
}
else if (b = find_helper()) {
return b;
}
}

So, please see if this code snippet compiles.

you made a mistake. when you write "const char* pc", you mean "*pc" is
const, that is, you can't do "pc[3]='a';", but you can still assign
another value to pc itself, "pc=AnotherValue;" can compile with no
error.

if you write "char* const b=find_help...", the compiler will announce
the error correctly.
 
O

Oplec

Neil said:
Oplec said:
I thought the code provided in my original post was an example of what
was compiling in my code, but it turns out it wasn't. I took apart my
program to code a function that compiles and reassigns to a const char*
and confirmed that it compiles with my current GCC. I took out all the
parts that didn't affect the irregularity and so the function isn't
practical, but in my code there is a recursive find function for a
binary tree. I took out all that stuff and what is below is what
compiles without error, but reassigns to a const. If you play around
with it, you will notice that if const char* b = 0 is used, it works,
but if const char* b = 7 is used, an error results in that line.

const char* find_helper()
{
if (const char* b = find_helper()){
return b;
}
else if (b = find_helper()) {
return b;
}
}

So, please see if this code snippet compiles.


you made a mistake. when you write "const char* pc", you mean "*pc" is
const, that is, you can't do "pc[3]='a';", but you can still assign
another value to pc itself, "pc=AnotherValue;" can compile with no
error.

if you write "char* const b=find_help...", the compiler will announce
the error correctly.

I understand now.

Thanks for the help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top