Articles & Books

Why is noexcept checked dynamically? -- StackOverflow

Quick A: Because exceptions occur (or not) dynamically.

Why is C++0x's noexcept checked dynamically?

I am curious about the rationale behind noexcept in the C++0x FCD. throw(X) was [deprecated], but noexcept seems to do the same thing. Is there a reason that noexcept isn't checked at compile time? It seems that it would be better if these functions were checked statically that they only dcalled throwing functions within a try block.

What's the difference between push_back vs emplace_back? -- StackOverflow

Quick A: When correctly implemented per the standard, you get in-place construction with perfect forwarding.

Longer question:

push_back vs emplace_back

I'm a bit confused regarding the difference between push_back and emplace_back.

 

void emplace_back(Type&& _Val);
void push_back(const Type& _Val);
void push_back(Type&& _Val);

As there is a push_back overload taking a rvalue reference I don't quite see what the purpose of emplace_back becomes?

What does std::function do that function pointers don't? -- StackOverflow

Quick A: A lot. They can bind to anything callable, not just functions. And they can perform conversions on parameter and return types.

Is there a use case for std::function that is not covered by function pointers, or is it just syntactic sugar?

The notation for std::function is quite nice when compared to function pointers. However, other than that, I can't find a use case where it couldn't be replaced by pointers. So is it just syntactic sugar for function pointers?

What new capabilities do user-defined literals add to C++? -- StackOverflow

Over the past year, UDLs have started to become available in some popular C++ compilers, including gcc 4.7 and Clang 3.1. As people are adopting those compilers in real code and starting to be ablel to use this feature, a natural question is how useful they are and how to use them:

What new capabilities do user-defined literals add to C++?

C++11 introduces user-defined literals which will allow the introduction of new literal syntax based on existing literals (int, hex, string, float) so that any type will be able to have a literal presentation.

Examples:

// imaginary numbers
std::complex<long double> operator "" _i(long double d) // cooked form
{
    return std::complex<long double>(0, d);
}
auto val = 3.14_i; // val = complex<long double>(0, 3.14)

// binary values
int operator "" _B(const char*); // raw form
int answer = 101010_B; // answer = 42

// std::string
std::string operator "" _s(const char* str, size_t /*length*/)
{
    return std::string(str);
}
auto hi = "hello"_s + " world"; // + works, "hello"_s is a string not a pointer

// units
assert(1_kg == 2.2_lb); // give or take 0.00462262 pounds

At first glance this looks very cool but I'm wondering how applicable it really is, [...] do you feel this feature will justify itself? What other literals would you like to define that will make your C++ code more readable?

Quick Q: Why isn't std::initializer_list a core-language built-in? -- StackOverflow

Quick A: Because it doesn't have to be. It's "the C++ way" to prefer library solutions, and initializer_list shows how far you can get with a pure library solution, then the rest of the way with minimal language support to create initializer_list objects.

Recently on SO:

Why isn't std::initializer_list a core-language built-in?

It seems to me that it's quite an important feature of C++11 and yet it doesn't have its own reserved keyword (or something alike).

 

Instead, initializer_list it's just a template class from the standard library that has a special, implicit mapping from the new braced-init-list {...} syntax that's handled by the compiler.

At first thought, this solution is quite hacky. ...

The Importance of std::function -- Malte Skarupke

See also "Generalizing Observer", written ten years ago when std::function was first adopted.

The Importance of std::function

by Malte Skarupke

... Classically an update loop would be implemented something like this: ...

And this is a good solution at first. But you’ve got that inheritance in there, and that doesn’t scale. You will probably want to add a render loop and maybe a separate update loop for the editor. Once you’ve got that many base classes it makes sense to combine them into one. And now you’ve just started on the way of having a big base class that gets used everywhere. Soon enough adding something different to the update loop involves a whole lot of unnecessary work.

Here is an alternate update loop using std::function: ...

 

Quick Q: How to initialize a const object (say vector) with complex initialization? -- StackOverflow

Quick A: With a lambda function. Try const mytype myobj{ []{ /* compute value */ return value; } }.

How would you initialize a const vector of function results using C++11?

Is it possible to use something like generate_n to create a const vector of, say, random numbers? I couldn't think of a way to do it without deriving vector and doing the assignment in the constructor.

C++ and Xcode 4.6 -- from Marshall Clow

Marshall Clow gives some helpful tips for C++ programmers migrating to to XCode 4.6:

C++ and XCode 4.6

So, you’ve installed Xcode 4.6, and you are a C++ programmer.

You want to use the latest and greatest, so you create a new project, and add your sources to the project, and hit Build, and … guess what? Your code doesn’t build!

Continue reading...