basics

Quick Q: What type do lambdas get compiled into? -- StackOverflow

Quick A: A compiler-generated type that stores the captured variables in its data members, and exposes the function signature and body as its operator().

Recently on SO:

What type do lambdas get compiled into?

As I know all data types must be known at compile time, and lambda is not a type. Does lambda got translated into anonymous struct with operator() or std::function wrapped?

For example,

std::for_each(v.begin(), v.end(), [](int n&){n++;});

Urbana Proposals - C++17 insight?

I've started with my series on the proposals for the next C++ Committee Meeting:

Urbana Proposals - C++17 insight?

by Jens Weller

From the article:

A short series to give you an overview over the Papers submitted in the latest mailing for the C++ Committee Meeting in Urbana-Champaign in Illinois. At the beginning of November the C++ Committee will have its 3rd Meeting this year. As C++14 is now finished, the focus is clearly on the upcoming C++17 standard.

Quick Q: Does unordered_map<string,MyClass>::erase() destroy my MyClass objects? -- SO

Quick A: No, but unordered_map<string, unique_ptr<MyClass>>::erase and unordered_map<string, shared_ptr<MyClass>>::erase do.

Today on SO:

std::unordered_map<std::String, myClass*> -- does std::unordered_map::erase() call myClass' DTor?

Assume I have some unordered_map of pointers to class instances, would erasing an object from that map also delete the instance?

(rewording the question:) If I wanted to delete that instance, which version would be right?

if(it != map.end())
{
    delete it->second;
    map.erase(it);
}

or simply

if(it != map.end())
    map.erase(it);

?

Quick Q: Why is "want speed? pass by value" not recommended? -- StackOverflow

Quick A: Because it performs worse than C++98 for common types like string and vector. The preferred way to optimize for rvalues is to add a && overload.

Fresh on SO:

Why is value taking setter member functions not recommended in Herb Sutter's CppCon 2014 talk (Back to Basics: Modern C++ Style)?

In Herb Sutter's CppCon 2014 talk Back to Basics: Modern C++ Style he refers on slide 28 (a web copy of the slides are here) to this pattern:

class employee {
  std::string name_;
public:
  void set_name(std::string name) noexcept { name_ = std::move(name); }
};

He says that this is problematic because when calling set_name() with a temporary, noexcept-ness isn't strong (he uses the phrase "noexcept-ish").

Now, I have been using the above pattern pretty heavily in my own recent C++ code mainly because it saves me typing two copies of set_name() every time -- yes, I know that can be a bit inefficient by forcing a copy construction every time, but hey I am a lazy typer. However Herb's phrase "This noexcept is problematic" worries me as I don't get the problem here: std::string's move assignment operator is noexcept, as is its destructor, so set_name() above seems to me to be guaranteed noexcept. I do see a potential exception throw by the compiler before set_name() as it prepares the parameter, but I am struggling to see that as problematic.

Later on on slide 32 Herb clearly states the above is an anti-pattern. Can someone explain to me why lest I have been writing bad code by being lazy?

Quick Q: Why did C++ add delegating constructors? -- StackOverflow

Quick A: Because they reduce code duplication, and so make code more readable and maintainable.

Recently on SO:

Why did C++11 introduce delegating constructors?

I cannot understand what the use is of delegating constructors. Simply, what cannot be achieve without having delegating constructors?

It can do something simple like this

class M
{
 int x, y;
 char *p;
public:
 M(int v) : x(v), y(0), p(new char [MAX]) {}
 M(): M(0) {cout<<"delegating ctor"<<endl;}
};

But I don't see it, is it worth introducing a new feature for such a simple thing? May be I couldn't recognize the important point. Any idea?

Insights into new and C++

I've written down some basic thinking on new and new standards:

Insights into new and C++

by Jens Weller

From the article:

Every now and then, I've been thinking about this. So this blogpost is also a summary of my thoughts on this topic, dynamic memory allocation and C++. Since I wrote the blog entries on smart pointers, and C++14 giving us make_unique, raw new and delete seem to disappear from C++ in our future code...

Quick Q: Do smart pointers help replace raw pointers? -- StackOverflow

Quick A: Yes, smart pointers replace owning raw pointers, and you should prefer smart pointers in new code. Raw pointers and references are still appropriate to pass parameters down a stack.

Recently on SO:

C++ 11 Smart Pointer usage

I have a question about smart pointers in c++ 11. I've started to have a look at C++ 11 (I usualy program in c#) and read some thing about smart pointers. Now i have the question, does smart pointers completely replace the "old" style of pointers, should i always use them?

Quick Q: Why can I return a unique_ptr by value? -- StackOverflow

Quick A: Because return local_obj; automatically treats it as an rvalue. After all, you won't be using it any more.

When this FAQ came up again recently on SO, the answer was to refer to this previous Q&A:

Returning unique_ptr from functions

unique_ptr<T> does not allow copy construction, instead it supports move semantics. Yet, I can return a unique_ptr<T> from a function and assign the returned value to a variable...

unique_ptr<int> foo()
{
  unique_ptr<int> p( new int(10) );

  return p;                   // 1
  //return move( p );         // 2
}

The code above compiles and works as intended. So how is it that line 1 doesn't invoke the copy constructor and result in compiler errors? If I had to use line 2 instead it'd make sense (using line 2 works as well, but we're not required to do so)....