What will you see if compile and run following simple program (first try answering in mind before actually compiling it)? 🙂

int main() {
    std::cout << (sizeof(char *) == 8) ? "64-bit" : "32-bit";
    return 0;
}
Advertisements

You can view all the predefined macros through command-line switches with some compilers. This works with gcc and icc (Intel’s C++ compiler):

$ touch empty.cpp

$ g++ -E -dM empty.cpp | sort >gxx-macros.txt

$ icc -E -dM empty.cpp | sort >icx-macros.txt

$ touch empty.c

$ gcc -E -dM empty.c | sort >gcc-macros.txt

$ icc -E -dM empty.c | sort >icc-macros.txt

The complexity of std::list::size() operation is O(N) while for other standard containers (vector, queue) and std::string it is O(1).

This is caused by splice() operation specific to lists that takes some range of elements from one list and inserts it to the specified position of another one.

This splicing is done with O(1) complexity and is extremely useful in some cases but it blocks all the opportunities for storing/knowing list length.

Pure virtual function can have body in base class too.

#include <iostream>

struct A
{
    virtual void foo() = 0;
};

void A::foo() {
    std::cout << "A::foo()" << std::endl;
}

struct B : A
{
    virtual void foo()
    {
        A::foo();
        std::cout << "B::foo()" << std::endl;
    }
};

int main()
{
    B b;
    b.foo();
    return 0;
}