Mandatory template arguments

Consider the following function declaration:

Though the declaration may seem rather involved, the actual application of this function is quite simple:

The compiler knows how to create an instance of  print_as_csv by a mechanism called template argument deduction. It works only for functions, because the compiler needs arguments to deduce the types from. There are, however, template functions for which not all template arguments can be deduced by the compiler.

Continue reading “Mandatory template arguments” »

Underprivileged unique pointers: retrofitting make_unique

std::shared_ptr<Value> is one of the starlets in C++11′s recently polished standard template library. shared_ptrs work just like normal C-style pointers except that they keep track of how many pointers point to the same object. Once the last shared_ptr pointing to an object goes out of scope, the object pointed at will be deleted. For convenience, the standard library offers the factory function std::make_shared<Value>():

std::make_shared<Value>() creates a new object of type Value, where the arguments given to make_shared() are forwarded to the constructor of Value. The newly created object is immediately wrapped in a std::shared_ptr<Value>, which is then returned to the caller.

Continue reading “Underprivileged unique pointers: retrofitting make_unique” »