GotW #1 Solution, and GotW #2: Temporary Objects
I've just posted the solution to GotW #1, and problem #2 for discussion. I hope you enjoy them.
June 17-20, Folkestone, UK
September 12-18, Aurora, CO, USA
November 16-21, Búzios, Rio De Janeiro, Brazil
November 26-28, Berlin, Germany
By Herb Sutter | May 9, 2013 09:52 AM | Tags: None
I've just posted the solution to GotW #1, and problem #2 for discussion. I hope you enjoy them.
By Blog Staff | May 9, 2013 06:56 AM | Tags: basics
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::vectorcan be initialized as
std::vector<std::string> words1 {"the", "frogurt", "is", "also", "cursed"}; refNow 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?
By Blog Staff | May 8, 2013 01:27 PM | Tags: basics
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, andreturnRValueRef()function.[...] AClass && returnRValueRef() { AClass a1(4); return move(a1); } int main() { AClass a; a = returnRValueRef(); }
By Blog Staff | May 8, 2013 09:22 AM | Tags: intermediate
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
Tis 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.
By Blog Staff | May 7, 2013 11:35 AM | Tags: basics
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: ...
By Blog Staff | May 6, 2013 11:26 PM | Tags: intermediate basics
Head on over to the C/C++ Cafe for:
Introduction to the C++11 feature: extended friend declaration
by FangLu
Excerpt:
The extended
frienddeclaration 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. ...
By Blog Staff | May 6, 2013 06:46 PM | Tags: intermediate basics
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"...
By Blog Staff | May 3, 2013 04:41 PM | Tags: basics
A 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. ...
By Blog Staff | May 2, 2013 01:13 PM | Tags: basics
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::ignorefrom 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 isinsertedset 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.
By Blog Staff | May 2, 2013 11:16 AM | Tags: intermediate basics
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? ]]