basics

Quick Q: Can an enum class be converted to the underlying type? -- StackOverflow

Quick A: Yes, but it requires an explicit cast. One of the key differences between enum and enum class: the latter has no implicit conversion to the underlying type.

Can an enum class be converted to the underlying type?

Is there a way to convert an enum class field to the underlying type? I thought this would be automatic, but apparently not.

enum class my_fields : unsigned { field = 1 };

unsigned a = my_fields::field;

That assignment is being rejected by GCC. error: cannot convert 'my_fields' to 'unsigned int' in assignment.

Quick Q: Why do 'auto x = my_x;' and 'X& x = my_x;' declare different types? -- StackOverflow

Quick A: Remember that you can add things like const and & to an auto type...

'auto' and explicit variable declaration behaving differently

I have something like this:

[...]

auto bar1 = foo.GetBar();
auto bar2 = foo.GetBar(); //address of bar2 != address of bar1. why?

Bar& bar3 = foo.GetBar();
Bar& bar4 = foo.GetBar(); //address of bar3 == address of bar4.

What's going on here?

In a nutshell: To move or not to move -- Martin Moene

[Ed.: If you're new to C++11 and wondering about move semantics, but find the longer "introductory" articles about it a little too detailed, here's a nice nutshell discussion with a simple example and good links for further reading.]

An introductory discussion of the new C++11 rvalue references in connection with move semantics:

To Move or Not To Move

by Martin Moene

I'm probably not the only one who needs some time to become familiar with the new C++11 rvalue references. Recall std::auto_ptr to take care of a resource's lifetime. It could catch you by surprise and cause the loss of something valuable with its unconditional move behaviour.

 

Well that's exactly why things changed in C++11.

Here are a few excerpts that helped me to put the new rvalue references in perspective. ...

Continue reading...

Quick Q: Can a lambda function be recursive? -- StackOverflow

Quick answer: Yes, use std::function.

This recent short question shows a concise and correct answer, along with a link to a previous more complex example.

Can lambda functions be recursive?

Here is a plain old recursive function:

int fak(int n)
{
    return (n <= 1) ? 1 : n * fak(n - 1);
}

How would I write such a recursive function as a lambda function?

[](int n) { return (n <= 1) ? 1 : n * operator()(n - 1); }
// error: operator() not defined

[](int n) { return (n <= 1) ? 1 : n * (*this)(n - 1); }
// error: this wasn't captured for this lambda function

Is there any expression that denotes the current lambda so it can call itself recursively?

Quick Q: If I have a T&&, why would I write std::move? -- StackOverflow

Here's a common question about the relationship between && and std::move. It includes links to related variations of the question -- aka duplicates, though they're not always exact duplicates and it's helpful to see answers on different aspects of the same core question.

In C++11, why use std::move when you have &&?

I recently attended a C++11 seminar and the following tidbit of advice was given.

when you have && and you are unsure, you will almost always use std::move

Could any one explain to me why [when you already have a T&& -- Ed.] you should use std::move as opposed to some alternatives and some cases when you should not use std::move?

Quick Q: How to use range-based for with std::map? -- StackOverflow

A common question from the SO archives:

C++11: How to use range-based for loop with std::map?

The common example for C++0x range-based for() loops is always something simple like this:

std::vector<int> numbers = { 1, 2, 3, 4, 5, 6, 7 };

for ( auto xyz : numbers )

{

     std::cout << xyz << std::endl;

}

In which case xyz is an int. But, what happens when we have something like a map? What is the type of the variable in this example:

std::map< foo, bar > testing = { /*...blah...*/ };

for ( auto abc : testing )

{

    std::cout << abc << std::endl;         // ? should this give a foo? a bar?

    std::cout << abc->first << std::endl;  // ? or is abc an iterator?

}

When the container being traversed is something simple, it looks like range-based for() loops will give us each item, not an iterator. Which is nice...if it was iterator, first thing we'd always have to do is to dereference it anyway.

 

But I'm confused as to what to expect when it comes to things like maps and multimaps.

C++11 Rocks: Visual Studio 2012 Edition -- Alex Korban

Alex Korban has written a nice e-book that covers the parts of C++11 available in Visual Studio, including documenting limitations and bugs while still focusing on how using the C++11 features makes code cleaner, safer, and faster into the bargain.

The Visual Studio 2012 edition is now in beta. See the table of contents for what's covered, and a free sample to check out the style.

C++11 Rocks: Visual Studio 2012 Edition

by Alex Korban

Highlights from the description:

Visual Studio 2012 gives you the opportunity to use C++11 features to make your code significantly cleaner and easier to read, and to improve performance as well.

But which features are there? Are they ready for use in production code? ... 

You can master the C++11 features in VS2012 with this book. It’s laser focused on C++11, Visual Studio 2012, and nothing else. You’ll quickly get in-depth knowledge of the stuff you need to know.

You’ll learn easily with tons of examples. I spent a lot of time researching and testing, and as a result the book details many C++11 bugs and cases of non-standard behavior in Visual Studio.

Continue reading...

C++ Training at All Levels -- Leor Zolman

On-Site C++ Training at All Levels

by industry veteran Leor Zolman

 

Note: For a limited time, any 4- or 5-day training includes the C++11 Overview.

 

Our C++ and C seminars have been designed by some of the best-known, most effective C++ educators practicing today. In addition to materials created by Leor our C++ training repertoire features courses licensed from and supported by industry leaders Dan Saks and Stephen C. Dewhurst.

A Whirlwind Overview of C++11 (1/2-day, author: Leor Zolman)

Advanced C++  (Author: Stephen C. Dewhurst)

Effective C++ (3-, 4- and 5-day versions of courseware by Scott Meyers based on his books)

An Effective Introduction to the Standard Template Library (STL) (Author: Scott Meyers)

C++ for Non-C Programmers (Author: Leor Zolman)

C++ and Object-Oriented Programming (a.k.a. C++ for C Programmers). (Author: Dan Saks)

A sample unit of any course is available upon request.

Contact us today for more information or to schedule an on-site training at your location!

Quick Q: When should you make a type non-movable in C++11? -- StackOverflow

So C++11 has these cool move semantics... but when would you not implement move construction and move assignment for your type?

When to make a type non-movable in C++11?

I was surprised this didn't show up in my search results, I thought someone would've asked this before, given the usefulness of move semantics in C++11:

When do I have to (or is it a good idea for me to) make a class non-movable in C++11?

(Reasons other than compatibility issues with existing code, that is.)