basics

Insights into new and C++

I've written down some basic thinking on new and new standards:

Insights into new and C++

by Jens Weller

From the article:

Every now and then, I've been thinking about this. So this blogpost is also a summary of my thoughts on this topic, dynamic memory allocation and C++. Since I wrote the blog entries on smart pointers, and C++14 giving us make_unique, raw new and delete seem to disappear from C++ in our future code...

Quick Q: Do smart pointers help replace raw pointers? -- StackOverflow

Quick A: Yes, smart pointers replace owning raw pointers, and you should prefer smart pointers in new code. Raw pointers and references are still appropriate to pass parameters down a stack.

Recently on SO:

C++ 11 Smart Pointer usage

I have a question about smart pointers in c++ 11. I've started to have a look at C++ 11 (I usualy program in c#) and read some thing about smart pointers. Now i have the question, does smart pointers completely replace the "old" style of pointers, should i always use them?

Quick Q: Why can I return a unique_ptr by value? -- StackOverflow

Quick A: Because return local_obj; automatically treats it as an rvalue. After all, you won't be using it any more.

When this FAQ came up again recently on SO, the answer was to refer to this previous Q&A:

Returning unique_ptr from functions

unique_ptr<T> does not allow copy construction, instead it supports move semantics. Yet, I can return a unique_ptr<T> from a function and assign the returned value to a variable...

unique_ptr<int> foo()
{
  unique_ptr<int> p( new int(10) );

  return p;                   // 1
  //return move( p );         // 2
}

The code above compiles and works as intended. So how is it that line 1 doesn't invoke the copy constructor and result in compiler errors? If I had to use line 2 instead it'd make sense (using line 2 works as well, but we're not required to do so)....

Exceptions, error codes, and assertions in C++ -- Joseph Mansfield

mansfield.pngOne reasoned take on the various error reporting mechanisms in C++ and a policy for deciding when each is appropriate:

Exceptions, error codes, and assertions in C++

by Joseph Mansfield

From the article:

It can often be difficult to decide between the various methods of error reporting in C++. For example, some common advice is that exceptions should only be thrown in exceptional circumstances. Needless to say, this isn't particularly helpful. What exactly is an exceptional circumstance? An exception to what? If we throw assertions into the mix, this can become even more complicated.

In general, functions express a contract to the calling code...

The C++14 Standard -- Mark Nelson

ddj-nelson.PNGToday on Dr. Dobb's:

The C++14 Standard

by Mark Nelson

From the article:

Voting on the C++14 standard was completed in August, and all that remains before we can say it is officially complete is publication by the ISO. In this article, I will visit the high points of the new standard, demonstrating how the upcoming changes will affect the way you program, particularly when using the idioms and paradigms of Modern C++.

The committee seems intent on keeping the standards process in a higher gear than in the past. This means that C++14, having had just three years since the last standard, is a somewhat constrained release. Far from being disappointing, this is a boon for programmers because it means implementers have been able to push out compliance with the new features in real time. Yes, you can start using C++14 features today — nearly all of them if you are flexible on your tool chain...

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...