Skip to content

Instantly share code, notes, and snippets.

@quietfanatic
Created September 28, 2013 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quietfanatic/6746964 to your computer and use it in GitHub Desktop.
Save quietfanatic/6746964 to your computer and use it in GitHub Desktop.
// When using first-class functions, c++11 will silently coerce a function returning a value
// into a std::function returning a reference, thus creating a ticking time bomb.
#include <stdio.h>
#include <functional>
const int& f1 (std::function<const int& ()> func) {
return func();
};
int f () { return 4; }
int main () {
const int& p = f1(f);
printf("%d\n", p); // Prints 32767 or somesuch.
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment