basics

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? ]]

Quick Q: Does [=] capture all variables in scope? -- StackOverflow

Quick A: No.

A simple but important question:

C++11 lambda capture semantics

When I use [=] to indicate that I would like all local variables to be captured by value in a lambda, will that result in all local variables in the function being copied, or just all local variables that are used by the lambda?

So, for example, if I have:

vector<int> my_huge_vector(100000);
int my_measly_int;
some_function([=](int i){ return my_measly_int + i; });

Will my_huge_vector be copied, even though I don't use it in the lambda?

Quick Q: static constexpr variable vs. constexpr function? -- StackOverflow

With a nice Quick A by Morwenn that not only gives the right answer as of today, but is current with a feature voted into C++14 just two weeks ago that lets you drop the ()'s:

static constexpr variable vs. function

Is there a difference between declaring floating point constant as a static constexpr variable and a function as in example below, or is it just a matter of style?

class MY_PI
{
public:
    static constexpr float MY_PI_VAR = 3.14f;
    static constexpr float MY_PI_FUN() { return 3.14f; }
}

Quick Q: When should I use noexcept? -- StackOverflow

As C++11-compliant compilers start to roll out and be adopted, people want to know to best use new C++11 features, such as:

When Should I Really Use noexcept?

 

  1. There are many examples of functions that I know will never throw, but for which the compiler cannot determine so on its own. Should I append noexcept to the function declaration in all such cases? ... For which situations should I be more careful about the use of noexcept, and for which situations can I get away with the implied noexcept(false)?
  2. When can I realistically except to observe a performance improvement after using noexcept... Do modern compilers take advantage of noexcept in this way? If not, can I excect some of them to do so in the near future?

 

Quick Q: What Is the Difference Between set and unordered_set in C++? -- StackOverflow

A common Q with a nice concise A:

What is the difference between set and unordered_set in C++?

Came across this good question, which is similar but not at all same since it talks about Java, which has different implementation of hash-tables, [...] So what is the difference in C++ implementation of set and unordered_set? This question can be ofcourse extended to map vs unordered_map and so on for other C++ containers.

Here is my initial assessment...

Quick Q: How Can Use a Lambda Function as a Hash Function for unordered_map? -- StackOverflow

Quick A: Name the lambda (by assigning it to a variable), then decltype it.

People sometimes ask this, so it's worth putting out a quick link to the short answer:

How to use lambda function as hash function in unordered_map?

I wonder if it is possible to use lambda function as custom hash function for unordered_map in C++11? If so, what is the syntax?