string has wrong size

Started by
23 comments, last by visitor 14 years, 2 months ago
It's actually a well known problem with Box2d when using GCC.

Someone suggested redefining the macro to the __unused attribute or something like that. Do you know how that works?

I trust exceptions about as far as I can throw them.
Advertisement
There's some discussion of using the __unused attribute to fix the problem here.

I see that the issue has come up before, but do you know if an issue has ever been filed? I haven't checked myself, but you might look through the issue list to see if an issue was ever filed, and if so, what the outcome was. If no issue has been filed, I would recommend filing one (even though the author was apparently aware of the issue at one point, such things are easy to lose track of if they're not documented somewhere).
Quote:Original post by Storyyeller
Oh I guess I should be returning by value in this case.
I got confused because temporaries are const references


No, temporaries are values. You can pass them into a function that accepts a const reference, because of special rules. You can't return a reference, const or not, to a temporary, because a temporary will not outlive the function. You can't return a reference to a local variable, either, because it won't outlive the function either.
However, as wrong as it looks...

int Foo(){	int temp = 0;	return temp;}int main(){	const int & ir = Foo();	std::cout << ir;}


...is completely valid. Const references to temporary return values forces the return value to not be destroyed right away. It's more-or-less move semantics. It can come in handy sometimes.
May-be this helps to understand the latter scenario:
#include <cstdio>#include <cstdlib>struct X{    X() { std::puts("Default constructor"); }    X(const X&) { std::puts("Copy constructor"); }    ~X() { std::puts("Destructor"); }};X foo(){    std::puts("In foo");    if (std::rand() >= 0) { //this is a hack trying to suppress named return value optimization        X x;        std::puts("Returning from foo");        return x;    }    return X();}int main(){    {        std::puts("In main");        const X& x = foo();        std::puts("Leaving scope in main");    }    std::puts("Exiting");}


My output:

Quote:
In main
In foo
Default constructor
Returning from foo
Copy constructor
Destructor
Leaving scope in main
Destructor
Exiting


As you can see, when you leave foo, the local is destructed. But at the return, right before it goes out of scope, a copy is made. The reference binds to that copy and keeps the temporary alive until the const reference goes out of scope.

This topic is closed to new replies.

Advertisement