basics

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

 

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

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

Quick Q: How do I use std::tie and std::ignore? -- StackOverflow

How well do you know tie and ignore, especially to use the C++11 multiple return value idiom?

Please explain this code that uses std::ignore

I'm reading the documentation on std::ignore from cppreference. I find it quite hard to grasp the true purpose of this object, and the example code doesn't do it much justice. For example, in the below code, how and why is inserted set to true? It doesn't make much sense to me.

#include <iostream>
#include <string>
#include <set>
#include <tuple>

int main()
{
    std::set<std::string> set_of_str;
    bool inserted;
    std::tie(std::ignore, inserted) = set_of_str.insert("Test");
    if (inserted) {
        std::cout << "Value was inserted sucessfully\n";
    }
}

If someone can explain the code to me, it would be appreciated. Thanks.

Quick Q: Why does shared_ptr of void work? -- StackOverflow

Quick A: When it comes to destruction, only your deleter knows for sure... but he's captured at construction time, so all starts well and stays well.

Why do std::shared_ptr<void> work

I found some code using std::shared_ptr to perform arbitrary cleanup at shutdown. At first I thought this code could not possibly work, but then I tried the following:

[edited]

int main() {
  vector<shared_ptr<void>> v;
  {
    v.push_back( shared_ptr<test>( new test() ) );
  }
} // [[ how can this destroy type-safely? ]]