basics

C++ Templates series -- Feabhas

Here's a recent series that just got a new instalment today: It introduces template basics in a nicely explained and accessible way suitable for a gentle introduction, and then going on to progressively help the reader develop stronger template muscles.

C++ Templates series

by Feabhas

An Introduction to C++ Templates

Template Classes

Template Inheritance

Templates and Polymorphism

Template Member Functions

Variadic Templates

Templates of Templates

And today: Template Specialization

From the Introduction:

Templates are a very powerful -- but often very confusing -- mechanism within C++. However, approached in stages, templates can be readily understood (despite their heinous syntax).

The aim of this series of articles is to guide beginners through the syntax and semantics of the foundation concepts in C++ template programming.

atoi and itoa conversions in C++11 -- FangLu

All your friends know about C++11's new stoi and to_string, right? If not, here's a quick refresher to share:

atoi and itoa conversions in C++11

by FangLu

The key reminder from the article:

... The atoi and itoa conversions in C are not very satisfying to programmers, because programmers need to deal with invalid input and exceptions to avoid worst case. On the other hand, these functions are straightforward and easy to use. So they are not rare in C++ code...

In C++11, global functions, such as std::to_string, std::stoi/stol/stoll are introduced to implement atoi/itoa conversions conveniently. For example:

string s;

s += to_string(12) + " is int, ";

s += to_string(3.14f) + " is float.";

cout << s << endl;

where to_string can do type conversion according to the parameter type.

Here is another example:

string s("12");

int i = stoi(s);

cout << i << endl;

Journey to the Alps: Participating in the C++ Standardization Process

Recently on the Spot blog:

Trip Report: Journey to the Alps -- Participating in the C++ Standardization Process

by Nathan Wilson

From the article:

... At Spot Trading, we currently utilize many of the C++11 features -- lambdas, keyword auto, nullptr, move operations, constexpr, range based for loops, explicit overrides, atomic operations, threading, futures, and new pointer types. Bjarne Stroustrup has said that we want to be able to continue to make simple things simple. These features of the language, mentioned above, certainly do that...

auto considered awesome -- Jarryd Beck

Today's op-ed from our Australian correspondent:

auto considered awesome

by Jarryd Beck

From the article:

 

My last post about why you might not want to use auto may have left some people thinking that I think you shouldn’t use it. In fact I think you should almost always use it...

 

C++11/14 Idioms I Use Every Day -- Paul Cechner

Start your Monday the right way by sending this to three of your friends who are new to (modern) C++:

C++11/14 Idioms I Use Every Day

by Paul Cechner

From the article:

Most attention on the new C++ has focused on the changes that provide functionality and performance that was previously not possible, both library enhancements (chrono, regex, smart pointers, and stuff to help with lambdas for example) and core language enhancements (perfect forwarding, variadic templates, the new memory model and threading capabilities, initialiser lists and the like). This functionality will impact us all in helping to write more correct code and efficient libraries, but often will only be relevant in certain parts of our code.

But the first thing that struck me when I started using C++11 was the smaller features that I could take advantage of every time I put my fingers to the keyboard. These are the things that make code more concise and simple and allow me to present my intentions more clearly. ...

Quick Q: Does make_shared avoid an extra allocation for the reference counts? -- StackOverflow

A: Yes, make_shared is your friend!

Recently on SO:

What happens when using make_shared

I'm interested if these two lines of code are the same:

shared_ptr<int> sp(new int(1)); // double allocation?
shared_ptr<int> sp(make_shared<int>(1)); // just one allocation?

If this is true could someone please explain why is it only one allocation in the second line?

HTTP and HTTPS in Qt

How to handle HTTP and HTTPs requests in Qt

HTTP and HTTPs in Qt

by Jens Weller

From the article:

Last week I started to work on an old project again: My own feed reader. I found the code 2 weeks a go on an old USB Stick, and decided to refactor it into a useful state. This involved dealing with HTTP via QNetworkAccessManager.

A Clang edition of the C++11/14 Rocks book is now available

Korban's C++11/14 feature overview book now has a Clang edition, in addition to VS2013 and GCC:

Clang Edition of the C++11/14 Rocks Book

by Alex Korban

From the announcement:

Do you use Clang to compile C++? Would you like to know all about the C++11 and C++14 language features it supports?

You can read about them in the new edition of my C++11/14 Rocks book tailored to Clang.

...

For those who have the GCC edition of the book: you’ll already be familiar with all the C++11 content as GCC also has full C++11 support. However, the Clang edition has full C++14 coverage instead of an overview.

Non-Static Data Member Initializers -- 741MHz

In case you missed it:

Non-Static Data Member Initializers

by 741MHz

From the article:

...  This problem is addressed in C++11 by allowing non-static data members to be initialized along with a declaration. For example, the following syntax is allowed:

struct foo {
    double x = 1.23;
    int y = 1;
    int z = 2;
};

This also works well with multiple constructors. The class described above could now be simplified and made easier to maintain further down the road: ...