basics

GotW #4 Solution: Class Mechanics -- Herb Sutter

The solution to GotW #4 is now available:

GotW #4 Solution: Class Mechanics (updated for C++11/14)

by Herb Sutter

From the article:

... To see why, consider the following canonical forms for how operator+= and operator+ should normally be implemented for some type T.
T& T::operator+=( const T& other ) {
    //...
    return *this;
}

 

T operator+( T a, const T& b ) {
    a += b;
    return a;
}

Did you notice that one parameter is passed by value, and one by reference? That’s because if you’re going to copy from a parameter anyway, it’s often better to pass it by value, which will naturally enable a move operation if the caller passes a temporary object such as in expressions like (val1 * val2) + val3. We’ll see more on parameter passing in a future GotW. ...

.

GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) -- Herb Sutter

The solution to GotW #3 is now available:

GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) (updated for C++11/14)

by Herb Sutter

From the article:

With no other changes, simply using the standard find algorithm could do everything the range-based for loop did to avoid needless temporaries (and questions about them) [...] and it further increases our level of abstraction.

GotW #2 Solution: Temporary Objects -- Herb Sutter

The solution to GotW #2 is now available:

GotW #2 Solution: Temporary Objects (updated for C++11/14)

by Herb Sutter

From the article:

Because C++ naturally enables move semantics for returned values like this string object, there’s usually little to be gained by trying to avoid the temporary when you return by value. ... For example, if the caller writes auto address = find_addr( mylist, “Marvin the Robot” );, there will be at most a cheap move (not a deep copy) of the returned temporary into address, and compilers are allowed to optimize away even that cheap move and construct the result into address directly.

But what if you did feel tempted to try to avoid a temporary in all return cases by returning a string& instead of string? Here’s one way you might try doing it that avoids the pitfall of returning a dangling reference to a local or temporary object: [...] To demonstrate why this is brittle, here’s an extra question:

For the above function, write the documentation for how long the returned reference is valid.

Go ahead, we’ll wait. ...

The questions for #3 are posted for discussion:

GotW #3: Using the Standard Library (or, Temporaries Revisited)

 

 

Quick Q: What's the best way to return multiple values? -- StackOverflow

Another example of how adding a feature, in this case move semantics, makes using the language simpler:

Interface for returning a bunch of values

I have a function that takes a number and returns up to that many things (say, ints). What's the cleanest interface?

  1. Return a vector<int>. The vector would be copied several times, which is inefficient.
  2. Return a vector<int>*. My getter now has to allocate the vector itself, as well as the elements. There are all the usual problems of who has to free the vector, the fact that you can't allocate once and use the same storage for many different calls to the getter, etc. This is why STL algorithms typically avoid allocating memory, instead wanting it passed in.
  3. Return a unique_ptr<vector<int>>. It's now clear who deletes it, but we still have the other problems.
  4. Take a vector<int> as a reference parameter. The getter can push_back() and the caller can decide whether to reserve() the space. However, what should the getter do if the passed-in vector is non-empty? Append? Overwrite by clearing it first? Assert that it's empty? It would be nice if the signature of the function allowed only a single interpretation.
  5. Pass a begin and end iterator. Now we need to return the number of items actually written (which might be smaller than desired), and the caller needs to be careful not to access items that were never written to.
  6. Have the getter take an iterator, and the caller can pass an insert_iterator.
  7. Give up and just pass a char *. smile

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();
}

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

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