• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
gasto

&++x Lvalue but &x++ not?

17 posts in this topic

In the [b]6.2.1 Results[/b] section page on 84 chapter for of the C++ programming language by Bjarne Stroustrup:

int* p = &++x; // p points to x
int* q = &(x++); // error: x++ is not an lvalue (it is not the value stored in x)

 

Why is the second statement's x++ not an lvalue?

According to the precedence chart, both postfix and prefix unary increment operators are executed before the address operator, and until the sequence point is reached(semicolon in both cases) the side-effect is executed([i]x+=1[/i].)

In other words, if the first statement's x is considered an lvalue, so should the second statment's x.

 

It would seem that x++ leaves the rvalue(whatever literal value x refers to, AKA referent) while ++x leaves the lvalue(referer) as the immediate evaluation.

0

Share this post


Link to post
Share on other sites

The post-increment is equal to the following code segment:

int postincrement(int& x)
{
   int oldX = x;
   x+=1;
   return oldX;
}

Hope this makes it more clear why x++ does not return an lvalue.

2

Share this post


Link to post
Share on other sites
Thats why preincrement is usually tiny bit faster than postincrement when used in for-loop. Because it doesn't need intermediate temporary variable to store the result of the operation.
0

Share this post


Link to post
Share on other sites

[...] consider that x++ is something like "increment and return the old value". The old value must necessarily be a temporary, so taking its address is a recipe for disaster, because by the time you want to do something with the pointer you just obtained, the temporary will be gone.

[...]

Ah, good mnemonic. I wonder if the register is incapable of loading both values(next and previous, AKA old and incremented) and therefore your idea of temporary rvalue(that will be deallocated after the next sequence point) actually happens.

0

Share this post


Link to post
Share on other sites

The post-increment is equal to the following code segment:

int postincrement(int& x)
{
   int oldX = x;
   x+=1;
   return oldX;
}

Hope this makes it more clear why x++ does not return an lvalue.

Yes, because it is an automatic variable whose scope and duration will expire(after the function returns.)

0

Share this post


Link to post
Share on other sites

 

it's in practice exactly the same, because the compiler sees that nothing uses the intermediate temporary and elides it.

 

Exactly. Have you got any assembly code to be examined to understand it a bit better?

0

Share this post


Link to post
Share on other sites

Just like x = x+5 won't work but x += 5 will work in a for loop

Edited by rip-off
When i tried it weeks ago, it didn't work but when i tried it after i posted that, it worked.
-1

Share this post


Link to post
Share on other sites


Just like x = x+5 won't work but x += 5 will work in a for loop 

Can you explain what you mean by [i]won't work[/i]?

0

Share this post


Link to post
Share on other sites

 

Thats why preincrement is usually tiny bit faster than postincrement when used in for-loop. Because it doesn't need intermediate temporary variable to store the result of the operation.


Only for types with a non-trivial preincrement/postincrement operator implementation, such as some iterators; for things like ints, it's in practice exactly the same, because the compiler sees that nothing uses the intermediate temporary and elides it.

 

 

I haven't said that it is somehow significantly faster, just pointed out the way it actually works.

0

Share this post


Link to post
Share on other sites

Just like x = x+5 won't work but x += 5 will work in a for loop [/size]

Can you explain what you mean by won't work?
When i tried it weeks ago, it didn't work but when i tried it after i posted that, it worked.
That's why it's edited out.
0

Share this post


Link to post
Share on other sites

That's ok. We'd prefer if you didn't edit earlier posts, as it can make the thread more difficult to follow for others. A better approach would be to edit the post and strike through any errors in the original, and add a explanation.

 

[url="http://www.gamedev.net/topic/649762-parse-and-querying/#entry5107199"]Here[/url] is an example where I corrected a mistake I made in this way. I'm going to restore your earlier post like that.

0

Share this post


Link to post
Share on other sites

That's ok. We'd prefer if you didn't edit earlier posts, as it can make the thread more difficult to follow for others. A better approach would be to edit the post and strike through any errors in the original, and add a explanation.

 

Here is an example where I corrected a mistake I made in this way. I'm going to restore your earlier post like that.

I think it would be beneficial if he stated what compiler he was using. I've used MinGW and GCC for years and both methods have always worked for me for as long as I can remember. This way we know if it is just a fluke that happened or if a compiler, for whatever reason, is playing (for lack of a better word) willy-nilly with the standard and how the code works.

Edited by BHXSpecter
0

Share this post


Link to post
Share on other sites

What people are referring to as "a temporary value" is known in C/C++ as an rvalue. The postfix unary increment returns an rvalue, whereas the prefix unary increment returns an lvalue.

 

This can be seen if you run the following code, compiled with C++11:

#include <iostream>

void print( const int& value )
{
    std::cout << "this is an lvalue" << std::endl;
}
void print( const int&& value )
{
    std::cout << "this is an rvalue" << std::endl;
}

int main()
{
    int x = 0;
    print( ++x );
    print( x++ );
    return 0;
}

A more detailed explanation on lvalues and rvalues can be obtained here:

http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx

 

Another approach in explaining this behaviour can be done by examining unary increment overloads in a custom class:

class X {
    int m_Value;
public:
    X& operator++()
    {
        ++m_Value;
        return *this;
    }
    X operator++(int)
    {
        X tmp(*this);
        operator++();
        return tmp;
    }
};

Your example using the class:

X x;
X* p1 = &++x; // this calls operator++(), which returns a reference to itself (return *this;), i.e. getting the address is valid.
X* p2 = &(x++); // this calls operator++(int), which returns the temporary after incrementing, i.e. getting the address is invalid.
Edited by TheComet
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0