<commit_before>---
layout: post
title: Almost Always Auto style
date: 2015-06-21 01:17:24
categories: c++
---

With the rise of C++11, we see a paradigm shift in C++ from the so-called left-to-right declaration style. For instance, function return types can now be written right from the function name and the new `using` directive also has the type on the right. Variable declaration with `auto` is just a part of the larger change that is going on.

When declaring variables, consider the following style:

{% highlight c++ %}
// Stack Allocation
auto e = employee{empid};

// Heap Allocation
auto w = make_unique<widget>();

// Literal Suffixes (User-defined suffixes coming in C++14)
auto x = 42; // int 
auto x = 42.0f; // float
auto x = 42ul; // unsigned long
auto x = "42"s; // C++14, std::string
auto x = 42ns; // C++14, std::nano_seconds

// Function declarations and named lambdas
auto f(double) -> int { ... }
auto f = [=] (double) -> int { ... };

// Alias declarations
using dict = set<string>;

template <class T>
using vec = vector<T, myalloc>;
{% endhighlight %}

For more information about the new paradigm, visit <NAME>'s [blog](http://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/).<commit_msg>Update article<commit_after>---
layout: post
title: Almost Always Auto style
date: 2015-06-21 01:17:24
categories: c++
---

With the rise of C++11, we see a paradigm shift in C++ from the so-called left-to-right declaration style. For instance, function return types can now be written right from the function name and the new `using` directive also has the type on the right. Variable declaration with `auto` is just a part of the larger change that is going on. There are many advantages of using auto, including no implicit accidental conversion, removal of visual clutter, less refactoring overhead, consistency. Therefore, as a general rule, auto can and should be applied except in a few cases, when moving is either expensive or forbidden.

Examples of the right-hand style and usage of auto:

{% highlight c++ %}
// Stack Allocation
auto e = employee{empid};

// Heap Allocation
auto w = make_unique<widget>();

// Literal Suffixes (User-defined suffixes coming in C++14)
auto x = 42; // int 
auto x = 42.0f; // float
auto x = 42ul; // unsigned long
auto x = "42"s; // C++14, std::string
auto x = 42ns; // C++14, std::nano_seconds

// Function declarations and named lambdas
auto f(double) -> int { ... }
auto f = [=] (double) -> int { ... };

// Alias declarations
using dict = set<string>;

template <class T>
using vec = vector<T, myalloc>;

// Not applicable for:
auto arr = std::array<int, 1000>{}; // Expensive to move
auto lock = std::lock_guard<std::mutex>{m} // Not movable
{% endhighlight %}

For more information about the new paradigm, visit <NAME>'s [blog](http://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/).
{'fraction_non_alphanumeric': 0.12487064505001724, 'fraction_numerical': 0.031045187995860642, 'mean_word_length': 3.8576214405360134, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2478441', 'n_tokens_mistral': 957, 'n_tokens_neox': 855, 'n_words': 374}