Const variable assignment

33 views
Skip to first unread message

Iftekhar Chowdhury

unread,
Nov 25, 2012, 9:50:20 AM11/25/12
to uoft_csc...@googlegroups.com
Hi Peter,

Does the following code look semantically correct to you?

{
    int a = 1 + 1;
    const int b = a; //okay ?
}

Thanks,

Peter Goodman

unread,
Nov 25, 2012, 9:55:53 AM11/25/12
to uoft_csc...@googlegroups.com

This is answered in the spec. Please read it more carefully.

Sent from my mobile device.

--
 
 

Iftekhar Chowdhury

unread,
Nov 27, 2012, 2:26:06 PM11/27/12
to uoft_csc...@googlegroups.com
Hello Peter,

Is it okay to assign const variable to a non-const variable ?

{
     const int a = 1;
     int b = a; //okay?
}

Thanks,

Peter Goodman

unread,
Nov 27, 2012, 2:46:35 PM11/27/12
to uoft_csc...@googlegroups.com
Yes. Please read the assignment spec closely: if it doesn't say something is an error, then it likely isn't.

Best Regards,

Peter Goodman,
http://www.petergoodman.me
65 High Park Ave.,
Toronto, Ontario
M6P 2R7


--
 
 

Peter Chen

unread,
Nov 27, 2012, 4:14:08 PM11/27/12
to uoft_csc...@googlegroups.com
The specs say it has to be a literal or uniform, so are variables allowed or not? whyyy are the semantic rules changing every 12 hours!!!!!! 

Peter Goodman

unread,
Nov 27, 2012, 4:25:35 PM11/27/12
to uoft_csc...@googlegroups.com
What spec are you reading? :-P

The spec says that const-qualified variables must be initialized with a literal value or with a uniform variable, and not an expression. In this case, b IS NOT const qualified, as it is not declared with a const qualifier.

I don't think the spec is ambiguous on this matter.

There might be other ambiguities, but your level of exclamation marks is excessive :-P

Best Regards,

Peter Goodman,
http://www.petergoodman.me
65 High Park Ave.,
Toronto, Ontario
M6P 2R7


--
 
 

Peter Chen

unread,
Nov 27, 2012, 5:25:52 PM11/27/12
to uoft_csc...@googlegroups.com
aren't the only uniform variables the 6 predefined ones?

when you declare a variable as constant, is that an uniform variable? or const type variable?

Peter Goodman

unread,
Nov 27, 2012, 5:51:40 PM11/27/12
to uoft_csc...@googlegroups.com
Peter,

Good question. The answer depends on if you are doing the bonus related to const variable initialization. If you are not doing the bonus, then treating programmer-defined const-qualified variables as uniform is incorrect. If you are doing the bonus, then treating them as uniform is fine.

Note: this is not an extension of the spec, just an implication of how different semantic rules and the assignment bonus questions interact.

Best Regards,

Peter Goodman,
http://www.petergoodman.me
65 High Park Ave.,
Toronto, Ontario
M6P 2R7


--
 
 

Reply all
Reply to author
Forward
0 new messages