Quick Q: How to use std:: pointers to implement data structures (say, a DAG)? -- StackOverflow

This is a good specific example of a very common question about how to use smart pointers in data structures:

weak_ptr VS shared_ptr in graph node parent list

I have a directed acyclic graph implemented by Graph and Node classes. Each node has a list of pointers to children and a list of pointers to parents. [...] The Child list uses std::shared_ptr so that nodes are kept in memory at least as long as they have parents. But I don't want a node to own its parents, so I used weak_ptr for the pointers to parents.

But then there was a problem with the algorithms...

 

Quick Q: Can an enum class be converted to the underlying type? -- StackOverflow

Quick A: Yes, but it requires an explicit cast. One of the key differences between enum and enum class: the latter has no implicit conversion to the underlying type.

Can an enum class be converted to the underlying type?

Is there a way to convert an enum class field to the underlying type? I thought this would be automatic, but apparently not.

enum class my_fields : unsigned { field = 1 };

unsigned a = my_fields::field;

That assignment is being rejected by GCC. error: cannot convert 'my_fields' to 'unsigned int' in assignment.

Quick Q: Why do 'auto x = my_x;' and 'X& x = my_x;' declare different types? -- StackOverflow

Quick A: Remember that you can add things like const and & to an auto type...

'auto' and explicit variable declaration behaving differently

I have something like this:

[...]

auto bar1 = foo.GetBar();
auto bar2 = foo.GetBar(); //address of bar2 != address of bar1. why?

Bar& bar3 = foo.GetBar();
Bar& bar4 = foo.GetBar(); //address of bar3 == address of bar4.

What's going on here?

In a nutshell: To move or not to move -- Martin Moene

[Ed.: If you're new to C++11 and wondering about move semantics, but find the longer "introductory" articles about it a little too detailed, here's a nice nutshell discussion with a simple example and good links for further reading.]

An introductory discussion of the new C++11 rvalue references in connection with move semantics:

To Move or Not To Move

by Martin Moene

I'm probably not the only one who needs some time to become familiar with the new C++11 rvalue references. Recall std::auto_ptr to take care of a resource's lifetime. It could catch you by surprise and cause the loss of something valuable with its unconditional move behaviour.

 

Well that's exactly why things changed in C++11.

Here are a few excerpts that helped me to put the new rvalue references in perspective. ...

Continue reading...

Quick Q: Can a lambda function be recursive? -- StackOverflow

Quick answer: Yes, use std::function.

This recent short question shows a concise and correct answer, along with a link to a previous more complex example.

Can lambda functions be recursive?

Here is a plain old recursive function:

int fak(int n)
{
    return (n <= 1) ? 1 : n * fak(n - 1);
}

How would I write such a recursive function as a lambda function?

[](int n) { return (n <= 1) ? 1 : n * operator()(n - 1); }
// error: operator() not defined

[](int n) { return (n <= 1) ? 1 : n * (*this)(n - 1); }
// error: this wasn't captured for this lambda function

Is there any expression that denotes the current lambda so it can call itself recursively?

cpp-netlib.org announced

From Dean Michael Berris:

cpp-netlib.org is here!

The project's new home is now at http://cpp-netlib.org/ (links to the old site should automatically redirect to this domain). We're looking at scaling the effort of the project and enforcing a few processes for consistency and for velocity. A few highlight to the website content:

  • A style guide. We are now getting to the point where we have a lot of code and more people sending in pull requests. In this light we've started a very minimalist style guide. We will discuss and update this as soon as we have more data to make decisions.
  • A development policy document. We've attempted to codify what we do and how we do it.
  • A proposal process. If you're considering getting involved or helping get your library developed or included in cpp-netlib, there's now a process for that.

We're also now ramping up our migration to use Google Test and getting ready to use Clang Format to use the Google style-guide formatting (just for the formatting) to make the code more consistent. I'm driving that process and getting the codebase into a more maintainable state. I've learned in these past few years that readable code makes my life easier, and contributors lives easier. Having said this we're not going to compromise on features of C++11 that we're going to use. I believe that now is the time to use C++11. ...

 

Continue reading...

RSS feed update: "News" vs. "All Posts"

If you're following the "News" category RSS feed, please switch to follow the "All Posts" RSS feed instead.

We used to cross-post nearly all blog items to the "News" category, which made it basically a synonym for "All Posts" and many of you used it that way. However, from now on the "News" category will only be used with selected "Top News/Highlights" style items that should  persist longer at the top of the home page in the section now titled "Recent Highlights."

To continue following all posts, please now use the "All Posts" feed instead. This feed is now also featured on the home page to make it easier to find.

Quick Q: If I have a T&&, why would I write std::move? -- StackOverflow

Here's a common question about the relationship between && and std::move. It includes links to related variations of the question -- aka duplicates, though they're not always exact duplicates and it's helpful to see answers on different aspects of the same core question.

In C++11, why use std::move when you have &&?

I recently attended a C++11 seminar and the following tidbit of advice was given.

when you have && and you are unsure, you will almost always use std::move

Could any one explain to me why [when you already have a T&& -- Ed.] you should use std::move as opposed to some alternatives and some cases when you should not use std::move?

Pattern-Oriented Software Architectures for Concurrent and Networked Software -- Doug Schmidt

A video preview is available for Doug Schmidt's upcoming free MOOC with Vanderbilt University via Coursera:

Pattern-Oriented Software Architectures for Concurrent and Networked Software

Next session: Starts March 4 (8 weeks long)

Workload: 4-6 hours/week

Douglas C. Schmidt is a Professor of Computer Science, Associate Chair of the Computer Science and Engineering program, and a Senior Researcher at the Institute for Software Integrated Systems, all at Vanderbilt University. He has also been the Chief Technology Officer for the Software Engineering Institute at Carnegie Mellon University, where he was responsible for directing the technical vision and strategic R&D investments.

C++ developers will know of Doug particularly because of his widely-acclaimed ACE and related libraries.

We asked Doug to provide an overview in his own words of what you can expect to see in the course and how it relates to C++11 in particular:

After a short ~45 minute intro to the topics covered in the course, the rest of the videos are divided into the following sections:

  • [~3 hours] Intro to concurrency and networking, which provides background info at the OS and middleware levels.  This part focuses on concepts and is largely language-neutral.
  • [~7 hours] Intro to patterns and frameworks, which provides coverage of pattern-oriented software architecture, with an emphasis on concurrent and networked software.  Small C++ and Java examples are shown throughout, though the main focus of this section is more on design techniques rather than programming, per se.
  • [~6 hours] Applying patterns and frameworks to develop concurrent and networked software, which examines *lots* of C++ code.  A high-performance HTTP web server is used as a running example to illustrate patterns and frameworks in practice.  80-90% of the focus is on C++ here, with some examples showing how you can do similar types of things in Java to demonstrate the generality of pattern and framework techniques.
  • [~3 hours] An appendix that provides an overview of C++ (including C++11 features), a case study of fundamental "Gang of Four" patterns that aren't directly related to concurrent and networked software, and other background information that may be of interest to some course participants.

Reminder: Registration deadline for spring ISO C++ meeting

Today on the committee reflector, the Bristol standards meeting host, Roger Orr, reminded us of the registration deadline.

This is also of interest to people who are not regular standards meeting. If you are in the Bristol area or otherwise interested in attending part of the standards meeting to see what's going on, you're welcome to attend -- see "How to Participate at Face-to--Face Meetings" on the Meetings and Participation page for more details.

Roger Orr writes:

Hello all, here is a reminder of booking details for the meeting.

 

Many of you have already booked for the Apr 15-20 (6-day) meeting of WG21 in Bristol, UK, and need read no further.

If you have not yet booked but are intending to come, please note that the room reservations at the hotel are time-limited. To ensure a room please book before the release date early in February.

 

  • The hotels will not claim the cost until the conference.
  • There is no time limit for registering for attendance if you are making other arrangements for accommodation.

The following link will take you to the WG21 tab of the ACCU booking site:

 

http://www.cvent.com/events/accu-2013/custom-22-09ec03b22c4f4a0a832e28126a4585fc.aspx

As mentioned in N3397the event is colocated with the ACCU conference (Apr 9-13).

You can book hotel accomodation at the time of registration: either in the conference hotel -- the Bristol City Centre  Marriott -- or the nearby Bristol Hotel (Cabot Circus) which is a little cheaper.

 

If you are only attending WG21 select registration type "WG21 conference only" in online registration.

If you are attending both the ACCU conference and the WG21 meeting you can book both at the same time (and therefore get continuous hotel room occupation) select registration type ACCU Member or ACCU Non-Member as appropriate.

 

The site also contains some tabs with information about Bristol, the hotels, and travel.

If you have any questions about Bristol please contact me (host), or Herb Sutter (convenor) as appropriate.

 

I look forward to seeing many of you in Bristol in April!

Regards,

Roger.