Can I access an object in C++ other than using an expression?
Asked Answered
E

3

7

According to C++03 3.10/1 every expression is either an lvalue or an rvalue. When I use = to assign a new value to a variable the variable name on the left of the assignment is an lvalue expression. And it looks like whatever I try to do with a variable it'll still use some expression.

Is there any way to manipulate a variable in C++ other than by using an expression?

Etch answered 10/12, 2012 at 11:51 Comment(15)
+1: like the language-lawyer tag.Pinnatifid
all value computation will involve expression. Type computations e.g. decltypes do not. But I am not sure if you are asking specifically about variable manipulations and hence value computationPinnatifid
Sounds a bit like doing something without doing something.Especial
@Christian Rau: Maybe. An answer like "no, there's no other way" would be just fine.Etch
does prefix increment count as an expression?Detraction
@Evgeni Yes, it has a type and a value (not sure if that are valid definition criteria though, but prefix increment is definitely an expression).Especial
@ChristianRau What isn't, then?Detraction
@Evgeni Don't know, I guess everything is an expression somehow, but in the end I'm not that well-versed in the depths of the language standard and its terminology (which is why I still up-voted this question and don't dare to write a trivial "no"-answer).Especial
@ChristianRau I thought beeing an expression had something to do with "beeing evaluated", for example having at least a =. Thats why I considered prefix ++ to be an exception, because the variable is logically only incremented, but not really read (at least not on the "C++-level")Detraction
@Evgeni I don't think evaluation implies having a = inside. In fact 1 + 2 is an arithmetic expression, too, since it can be evaluated (to 3 of type int), and so can ++a, having the type and value of a after the increment. In fact just writing a is an expression with the value and type of a (or an lvalue reference to a?).Especial
@ChristianRau You're right. My aplogies to sharptooth, but the longer I think about it,the sillier the question becomesDetraction
Real programmers use a magnetized needle and a steady hand. (xkcd.com/378)Forb
Heck, 1 is an expression, you don't need 1+2.Whitesmith
Does inline asm count as an expression? :>Stevens
@JonasWielicki, an asm block is a declaration, but you put string literal(s) inside it, and a literal is an expression.Contraption
W
2

The only way would be through a statement, yet not via an expression which is part of such a statement. An example would be a definition, std::string x;. This calls the default ctor on x. But does this count as a manipulation to you?

There aren't that many other statements, actually. Loop control statements cannot change objects themselves other than via side effects of the loop control expressions. goto, break and continue can't do it at all. throw is an expression and catch() can't change anything, so that pair is also irrelevant. I don't think there's any other non-expression-statement.

Whitesmith answered 10/12, 2012 at 15:19 Comment(3)
Hm, catch will assign the exception value to a variable if you ask it to do so, won't it?Stevens
Yes, but you would need C-x M-c M-butterfly to spontaneously generate an exception in that case. Otherwise you need an expression to cause the throw.Contraption
@JonasWielicki: No it won't, not in the strict sense of the word. It initializes a new variable. It can't modify (assign to) an existing variable.Whitesmith
C
2

You can set the value of a variable without using an expression, but you can't really choose what value it gets. The way I read appendix A of the C++11 standard (the language grammar), a declaration is not an expression. If you write int a; at function scope, a will be assigned an indeterminate value. If you write it at file scope, a will be assigned the value 0. But you can't assign it a value or pass constructor arguments because the act of doing so involves an expression.

Contraption answered 10/12, 2012 at 15:19 Comment(0)
B
0

Not sure if it answers your question strictly but you can manipulate a variable indirectly. E.g.:

int a;
int *pA = &a;
*pA = 5;

Here the value of a is changed but without any expression involving a. The expression involves only pA.

Other than that, there might be side effects of unrelated operations that result in variable change, either intentional or not (such as memory corruption that unintentionally changes some variable).

Barytone answered 10/12, 2012 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.