0% found this document useful (0 votes)
160 views17 pages

C++ 11 & 14 Features Overview

C++ 11 and C++ 14 introduced many new language and library features to C++ that improve developers' ability to write better code. Some key features discussed include auto keyword to simplify type declarations, lambda expressions to concisely write inline functions, uniform initialization syntax using curly braces, smart pointers like shared_ptr and unique_ptr to simplify memory management, and variadic templates to allow functions to take a variable number of arguments. Visual Studio has implemented support for many of these new C++ standards features over time to bring modern C++ capabilities to its compiler.

Uploaded by

Tamás Benyács
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views17 pages

C++ 11 & 14 Features Overview

C++ 11 and C++ 14 introduced many new language and library features to C++ that improve developers' ability to write better code. Some key features discussed include auto keyword to simplify type declarations, lambda expressions to concisely write inline functions, uniform initialization syntax using curly braces, smart pointers like shared_ptr and unique_ptr to simplify memory management, and variadic templates to allow functions to take a variable number of arguments. Visual Studio has implemented support for many of these new C++ standards features over time to bring modern C++ capabilities to its compiler.

Uploaded by

Tamás Benyács
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

C++ 11 and C++ 14

New Language And Library Features That

Will Make Your Code Better

Kate Gregory
www.gregcons.com/kateblog
@gregcons
C++ Standard
• For a long time, there was no standard at all
o Multiple compilers, mostly agreed with what Stroustrup wrote

• C++ 98
o Slightly tweaked in 2003
o Some people say C++ 98/03

• TR1 – technical report 1


o The parts of “C++ 0x” everyone could agree on
o Released in 2005
o Compilers started to implement parts they liked

• C++ 11 • Language
o What C++0x turned out to be
o Keywords, punctuation, syntax, parsing
• C++ 14
o Settled Feb 15th 2014 at Issaquah meeting
o Completes C++ 11 • Library
o std::
Visual C++
• Microsoft C++ 1 was Microsoft C 7.0, in 1992
o Over 20 years ago!
• VC1 was C++ 2
• … there was no VC3 (version # syncing) …
• VC9 was Visual C++ 2008, Visual Studio 2008
o VC9 SP1 implemented some TR1 features
• VC10 is Visual C++ 2010, Visual Studio 2010
o Lots of C++11 features are included
• VC11 is Visual C++ 11, Visual Studio 2012
o ALL library features
o Some/most language features
• VC12 is Visual C++ 12, Visual Studio 2013
o More language features (variadic templates!)
o Some C++ 14 features
The Big Deals
• auto • Uniform Initialization
o Productivity, readability o {} everywhere
o Maintenance
o Needed for lambdas
• shared_ptr,
unique_ptr
• Lambdas o Don’t delete stuff!
o Make standard algorithms o Also, new stuff less
usable
o Stack semantics (RAII) is your
o Concurrency friend
o Functional style
• Variadic templates
• Range-based for
auto
• If you know C# var, you know auto
• Still strongly typed – just not by you
• 3 major strengths:
o Annoying iterator declarations
o Unspeakable types
o Dependent types (again, iterators) in volatile code
• Most of what you don’t like about standard
containers and standard algorithms goes away with
auto
Tiny Functions
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

void print_square(int i)
{
cout << i*i << endl;
}

int main()
{
vector<int> v;
// vector gets filled
for_each(v.begin(), v.end(), print_square);
}
Why Does It Need a Name?
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
vector<int> v;
// vector gets filled
for_each(v.begin(), v.end(),
[](int i) { cout << i*i << endl; } );
}
Lambdas
• Three parts
o [] – “Hi, I’m a lambda” aka capture clause
o () – parameters (imposed by the caller)
o {} – body

• Capture clause is non-optional but can be empty


o [x]
o [&x]
o [=]
o [&]
o Can also mix and match

• May need to specify return type


o [](int x) -> int {/* stuff */}
Lambdas and Concurrency
• Parallel Patterns Library (ppl.h)
o concurrency::parallel_for
o concurrency::parallel_for_each

• C++ AMP (amp.h)


o concurrency::parallel_for_each

• Both take a lambda as a parameter


o Represents the work being spread across cores
Range for
• Most of the for_each you write are for the
whole container
o begin(v), end(v)
o v.begin(),v.end()

• Neater:
for(int elem: v)
{/*loop body*/}
• Note:
o Language keyword, not library function in std::
o auto works here too – try const auto& to avoid copies
Initialization
• Many ways to initialize built in types like int
o int a = 2;
o int b(2);

• Initializing C-style arrays could be done with {}


o But who uses C-style arrays now?

• To initialize an object, use a constructor


o Foo f = 3;
o Employee newHire(John, today + 1, salary);
o Employee CEO();
o Employee someone;

• Lots of different ways means confusion


o Especially for newcomers to the language
Uniform initialization
• Braces are always ok
o int a{2};
o Employee CEO{};
o Employee newHire {John,today+1,salary};
o vector<int> v {1,2,3,4};
o vector<Employee> staff {CEO,newHire};

• Consistent and easy to remember


• Can nest
 vector<Employee> company { CEO,
newHire,
{Mary, today+1, salary}
};
shared_ptr and unique_ptr
• Stop managing memory yourself
o Member variables or local objects you’re just using for a calculation
o Raw pointers are the wrong choice if lifetime is to be managed
• Fine for observation/reaching eg parent->Invalidate();

• Best choice: solid objects, stack semantics


o Even when passing to / returning from functions
o RVO, move semantics

• Lowest overhead smart pointer: unique_ptr


o Noncopyable, but movable
o Plays well with collections (move it in, move it out)

• OK with ref counting overhead: shared_ptr


o make_shared lowers overhead somewhat
Variadic
• Taking an unspecified number and type of arguments
• Function
o printf
• Macro
o Logging
• Templates
o make_shared, make_unique

auto sp1 = make_shared<int>(2);


auto sp2 = make_shared<Employee>(John, today+1,
salary);
History
• Variadic templates are in C++ 11
o Needed for many valuable library features
o Including make_shared
• Parts of C++ 11 appeared in VS 2010
o And some parts slightly earlier in a feature pack for 2008
• Variadic templates were not actually implemented in
Visual Studio until Visual Studio 2013
o But features relying on them were implemented earlier
• Before that the library implementation faked them with
macros
• Infinity was actually 10
o And for performance reasons infinity was later lowered to 5
• Now that VC++ has variadic templates, your builds will
be faster
std::tuple
• Like a std::pair, but any number of elements
• Saves writing little class or struct just to hold a clump
of values
• Create with uniform initialization
std::tuple<int, std::string, double>
entry { 1, "Kate", 100.0 };
• Or use std::make_tuple
o Makes auto possible
• To access or set values, use
std::get<position>(tupleinstance)
• Has comparison operators etc already implemented
C++ Renaissance?
• Some of us never left
• Some great tech coming from Microsoft:
o Writing Windows 8 store apps in C++/CX
o Leveraging the GPU without learning another language using
C++ AMP
o More parity with managed languages
• Ask people what they don’t like about C++
o Almost every answer gets “that’s different w/ C++ 11”
o No denying there’s a lot of punctuation, though
• What should you do next?
o Get Visual Studio 2013
o Try some C++ 11 and 14 features
o Try writing a Windows Store app
o Try using C++ AMP
o www.gregcons.com/kateblog

You might also like