Skip to content

Instantly share code, notes, and snippets.

@yang
Created December 2, 2008 01:02
Show Gist options
  • Save yang/30927 to your computer and use it in GitHub Desktop.
Save yang/30927 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class c {
public:
c() : c_(NULL) { cout << "c(" << c_ << ") this=" << this << endl; }
c(const c& c) : c_(&c) { cout << "c(" << c_ << ") this=" << this << endl; }
~c() { cout << "~c(" << c_ << ") this=" << this << endl; }
int f() { cout << "c(" << c_ << ").f() this=" << this << endl; return 0; }
private:
const c* c_;
};
template<typename T> void nop(const T &x) { cout << "nop()" << endl; }
int main() {
nop(c(c()).f());
return 0;
}
/*
I get:
c(0) this=0x7fff13f56120
c(0).f() this=0x7fff13f56120
nop()
~c(0) this=0x7fff13f56120
I'm expecting (where .... are non-zero values):
c(0) this=...
c(...) this=...
c(...).f() this=...
nop()
~c(...) this=...
~c(0) this=...
Some people have suggested that RVO is happening. However:
- would RVO take precedence over program semantics?
- I didn't use optimization flags (default is -O0)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment