May 2013

Quick Q: How can I make my type construct from { 1, 2, 3, 4 } like vector? -- StackOverflow

Quick A: Write a constructor that takes a std::initializer_list.

See the link for the nice longer answer:

how to implement an initializer list for user defined type? (analogus to std::vector initializer list)

std::vector can be initialized as

 

std::vector<std::string> words1 {"the", "frogurt", "is", "also", "cursed"};
ref

Now if want to achieve the similar functionality for one of my types how do I go about doing it? How should I implement the constructor for this functionality?

Quick Q: How does return by rvalue reference work? -- StackOverflow

Quick A: So easily that it's automatic... just return a value, not a reference.

How does return by rvalue reference work?

Just when I thought I kind of understand rvalue reference, I ran into this problem. The code is probably unnecessarily long, but the idea is quite simple. There is a main() function, and returnRValueRef() function.

[...]

AClass && returnRValueRef() {
  AClass a1(4);
  return move(a1);
}

int main() {
  AClass a;
  a = returnRValueRef();
}

Quick Q: What's the difference between vector<T...> and vector<T>...? -- StackOverflow

SO's version of the question added one layer of wrapping:

What's the difference between doing vector<vector<T...>> and vector<vector<T>...>

I saw code like this earlier:

 

using A = std::vector<std::vector<T>...>

where T is a variadic list of template arguments. I wanted to know what the difference is between putting the parameter pack at the end of the last angle bracket and the first. For example:

using B = std::vector<std::vector<T...>>;

Both of these two compile fine but I don't know what the difference is.

Can someone explain? Thanks.

Introduction To the C++11 Feature: Delegating Constructors -- sumi_cj

Head on over to the C/C++ Cafe for:

Introduction to the C++11 feature: delegating constructors

by sumi_cj

Excerpt:

In C++98, if a class has multiple constructors, these constructors usually perform identical initialization steps before executing individual operations. In the worst scenario, the identical initialization steps are copied and pasted in every constructor. See the following example: ...

Trip Report: ACCU 2013 and the C++ Standards Meeting -- Anthony Williams

Anthony Williams just posted another nice trip report on the recent standards meeting in Bristol, as well as a few words on the ACCU conference held back-to-back with the ISO meeting:

ACCU 2013 and the C++ Standards Meeting

by Anthony Williams

 

This year's ACCU conference was at a new venue: the Marriott hotel in Bristol. This is a bit closer to home for me than the previous venue in Oxford, which made the trip there and back more comfortable. As ever, the conference itself was enjoyable, educational and exhausting in equal measure.

 

This year was also BSI's turn to host the Spring ISO C++ committee meeting, which was conveniently arranged to be the week following ACCU, in the same hotel. Having not attended a meeting since the last time the committee met in the UK, I was glad to be able to attend this too. ...

Introduction To the C++11 Feature: Extended friend Declaration -- FangLu

Head on over to the C/C++ Cafe for:

Introduction to the C++11 feature: extended friend declaration

by FangLu

Excerpt:

The extended friend declaration feature is newly introduced in the C++11 standard. In this article, I will introduce this feature and provide some examples on how to use this feature.

 

Firstly, let's see why this feature is added into C++11. ...

 

GotW #1: Variable Initialization—or Is It? -- Herb Sutter

Herb Sutter is resuming his Guru of the Week series of problem-and-solution articles about coding in C++, with the intent to gradually update the 88 existing issues and write a few more along the way.

The first problem was posted today:

GotW #1: Variable Initialization -- or Is It? (3/10)

by Herb Sutter

This first problem highlights the importance of understanding what you write. Here we have a few simple lines of code — most of which mean something different from all the others, even though the syntax varies only slightly.

The solution is coming "soon"...

Clang/LLVM Conference videos and slides are now available

The recent 2013 European Clang/LLVM developer meeting talk videos and/or slides are now available here.

Here are a few of the talks of interest to C++ developers. All have videos available except the last. Check out the page for details and a full talk list.

Keynotes

Optimization in LLVM - Numbers, A Case Study, and Looking Forward
Chandler Carruth (Google)

Talks

clang-format - Automatic formatting for C++
Daniel Jasper (Google)

Performing Source-to-Source Transformations with Clang
Olaf Krzikalla (TU Dresden)

Run-time tracking of uninitialized data with MemorySanitizer
Evgeniy Stepanov (Google)

LLVM on IBM POWER processors: a progress report
Ulrich Weigand (IBM)

Tutorials

How to implement an LLVM Assembler
Simon Cook (Embecosm)

The Clang AST (slides only)
Manuel Klimek (Google)

For more talks, and video/slide links, head over to the site.

Ownership and 'Memory -- Andy Balaam

andy.PNGA short and basic summary of C++'s view of memory management: You can worry about it a lot less, and still be efficient, if you say who owns what.

Goodness in programming languages, part 4 -- Ownership & Memory

by Andy Balaam

From the post:

... over time the community of C++ programmers has been developing a new way of thinking about memory, and developing tools in the C++ language to make it easier to work in this way.

Modern C++ code rarely or never uses “delete” or “free” to deallocate memory, but instead defines clearly which object owns each other object. ...