using extern template (C++11) to avoid instantiation

You should only use extern template to force the compiler to not instantiate a template when you know that it will be instantiated somewhere else. It is used to reduce compile time and object file size. For example: // header.h template<typename T> void ReallyBigFunction() { // Body } // source1.cpp #include “header.h” void something1() { … Read more

What is std::move(), and when should it be used and does it actually move anything?

1. “What is it?” While std::move() is technically a function – I would say it isn’t really a function. It’s sort of a converter between ways the compiler considers an expression’s value. 2. “What does it do?” The first thing to note is that std::move() doesn’t actually move anything. It changes an expression from being … Read more

std::unique_ptr with derived class

If they are polymorphic types and you only need a pointer to the derived type use dynamic_cast: Derived *derivedPointer = dynamic_cast<Derived*>(basePointer.get()); If they are not polymorphic types only need a pointer to the derived type use static_cast and hope for the best: Derived *derivedPointer = static_cast<Derived*>(basePointer.get()); If you need to convert a unique_ptr containing a … Read more

Is writing to &str[0] buffer (of a std:string) well-defined behaviour in C++11?

Yes, the code is legal in C++11 because the storage for std::string is guaranteed to be contiguous and your code avoids overwriting the terminating NULL character (or value initialized CharT). From N3337, ยง21.4.5 [string.access] const_reference operator[](size_type pos) const; reference operator[](size_type pos); 1 Requires: pos <= size(). 2 Returns: *(begin() + pos) if pos < size(). … Read more

Advantage of using trailing return type in C++11 functions

In this example, they mean the exact same thing. However, there are a few advantages to using the trailing return type form consistently (Phil Nash calls these “East End Functions”, since the return type is on the east end). Using parameters. Obviously when using parameters to determine the return type, you must use a trailing … Read more

When is std::chrono epoch?

It is a function of both the specific clock the time_point refers to, and the implementation of that clock. The standard specifies three different clocks: system_clock steady_clock high_resolution_clock And the standard does not specify the epoch for any of these clocks. Programmers (you) can also author their own clocks, which may or may not specify … Read more

Initialization difference with or without curly braces in C++

Short version Initialization via {..} is list-initialization, which prohibits narrowing conversions. For example, if LLONG_MAX is the maximum value of an long long int, and your int cannot represent that: int x = LLONG_MAX; // probably accepted with a warning int x {LLONG_MAX}; // error Similarly: long long y = /*something*/; int x = y; … Read more

`static constexpr` function called in a constant expression is…an error?

As T.C. demonstrated with some links in a comment, the standard is not quite clear on this; a similar problem arises with trailing return types using decltype(memberfunction()). The central problem is that class members are generally not considered to be declared until after the class in which they’re declared is complete. Thus, regardless of the … Read more

tech