You can have lambdas in C++ with GCC and Boost even without C++11 support.

int val = 2;
int nums[] = {1, 2, 3};
int* end = nums + 3;
int* iter = std::find_if(nums, end,
    GCC_LAMBDA(const bind val, int num, return bool) {
        return num == val;
The GCC lambda function macros are implemented using local functions.
You can read more on this here.

Your C++ code may be much slower in reading lines from stdin than Python by default.

Assume following simple case:
// C++ code
while (std::cin) {
    getline(std::cin, input_line);
    if (!std::cin.eof()) {
# Python code
for line in  sys.stdin:
    count += 1

$ cat test_lines | ./readline_test_cpp
Saw 5570000 lines in 9 seconds.  Crunch speed: 618889

$cat test_lines | ./
Read 5570000 lines in 1 seconds. LPS: 5570000

Reason: by default, std::cin is synchronized with stdio, which causes it to avoid any input buffering. If you add this to the top of your main, you should see much better performance:

You can use cling, an interactive interpreter for the C++ language based on clang/LLVM, for quick testing of small code pieces.

[cling]$ #include
[cling]$ using namespace std;
[cling]$ int a[] = {1,2,3};
[cling]$ for (int& x: a){ x += 10; }
[cling]$ for (int& x: a){ cout << x << “,”; }
It supports both old C++03 and new C++11 standards fine!

The next code doesn’t call object’s destructor when exception is thrown if compiled with GCC or MS Visual C++:

#include <iostream>
#include <stdexcept>
struct A {
    A () { std::cout << "A" << std::endl; }
    ~A () { std::cout << "~A" << std::endl; }
void myfunc () {
    A temp;
    throw std::runtime_error("moo");
int main () {

This is caused by lack of matching catch() handler –if you add it destructor will be called properly.
Looking at the C++ standard, section 15.3 [except.handle] point 9, we see:
If no matching handler is found in a program, the function terminate()
(_except.terminate_) is called. Whether or not the stack is unwound
before calling terminate() is implementation-defined.
Both gcc and Visual Studio behave the same in this context, and do not perform full stack unwinding in this case.

You shouldn’t assign realloc() call result to the same variable that pointed to original memory block.

In case of failure the original pointer will not be released so you have to do it manually!
/* BAD! */
p = (char*) realloc(p, new_size);
if(!p) {
/* Good */
old_p = p;
p = (char*) realloc(old_p, new_size);
if(!p) {

decltype operation introduced in new C++11 standard can be used to define a variable/object of some type that another already existent variable/object has.


const int&& foo();
int i;
struct A { double x; };
const A* a = new A();
decltype(foo()) x1; // type is const int&&
decltype(i) x2; // type is int
decltype(a->x) x3; // type is double
decltype((a->x)) x4; // type is const double&

pSpeed pointer in the following code is equal to zero (0x00) in debugger but is casted to “true” for comparison (see program output).

#include <iostream>
using namespace std;
struct Car
    int speed;
int main()
    int Car::*pSpeed = &Car::speed; // = 0;
    if(pSpeed) {
           cout << "First!"<< endl;
    } else {
           cout << "Second!"<< endl;
    return 0;
If you try assigning zero explicitly instead the other branch of “if” statement will be chosen 😉