Compiling C++11 with g++

Flags (or compiler options) are nothing but ordinary command line arguments passed to the compiler executable. Assuming you are invoking g++ from the command line (terminal): $ g++ -std=c++11 your_file.cpp -o your_program or $ g++ -std=c++0x your_file.cpp -o your_program if the above doesn’t work.

C++ template typedef

C++11 added alias declarations, which are generalization of typedef, allowing templates: template <size_t N> using Vector = Matrix<N, 1>; The type Vector<3> is equivalent to Matrix<3, 1>. In C++03, the closest approximation was: template <size_t N> struct Vector { typedef Matrix<N, 1> type; }; Here, the type Vector<3>::type is equivalent to Matrix<3, 1>.

Returning unique_ptr from functions

is there some other clause in the language specification that this exploits? Yes, see 12.8 §34 and §35: When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object […] This elision of copy/move operations, called copy elision, is permitted […] in a return statement in a function … Read more

std::unique_lock or std::lock_guard?

The difference is that you can lock and unlock a std::unique_lock. std::lock_guard will be locked only once on construction and unlocked on destruction. So for use case B you definitely need a std::unique_lock for the condition variable. In case A it depends whether you need to relock the guard. std::unique_lock has other features that allow … Read more

How do I pass a unique_ptr argument to a constructor or a function?

Here are the possible ways to take a unique pointer as an argument, as well as their associated meaning. (A) By Value Base(std::unique_ptr<Base> n) : next(std::move(n)) {} In order for the user to call this, they must do one of the following: Base newBase(std::move(nextBase)); Base fromTemp(std::unique_ptr<Base>(new Base(…)); To take a unique pointer by value means … Read more

C++11 rvalues and move semantics confusion (return statement)

First example std::vector<int> return_vector(void) { std::vector<int> tmp {1,2,3,4,5}; return tmp; } std::vector<int> &&rval_ref = return_vector(); The first example returns a temporary which is caught by rval_ref. That temporary will have its life extended beyond the rval_ref definition and you can use it as if you had caught it by value. This is very similar to … Read more

What are the main purposes of using std::forward and which problems it solves?

You have to understand the forwarding problem. You can read the entire problem in detail, but I’ll summarize. Basically, given the expression E(a, b, … , c), we want the expression f(a, b, … , c) to be equivalent. In C++03, this is impossible. There are many attempts, but they all fail in some regard. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)