January 2013

Online C++ compilers


Many people don't realize how many web pages offer access to try out C++ compilers, including many of the latest compilers with burgeoning C++11 language support. So we thought we'd publish a list.

Do you:

  • want to try out C++, but don't have a compiler installed?
  • want to try out a C++11 feature your compilers don't yet support?
  • want to compare the results of compiling a test program using different compilers?

Then try one of these online compilers! Some are compile-only to let check whether your code is legal, and some let you also run your test programs to see their output. For each, we include a list of the compilers that the page currently supports -- they include the latest from Clang (3.2, Dec 2012), GCC (4.8.0 prerelease), Intel (13.0, Oct 2012), and Microsoft (VC++ alpha CTP, Nov 2012).

This list is now also available on the Get Started! page.

Quick Q: What can I do with a moved-from object?—StackOverflow

Quick A: It's a valid object with an unspecified state, so start by using member functions that have no preconditions. For example, assign a new value to the object.

What can I do with a moved-from object?

Does the standard define precisely what I can do with an object once it has been moved from? I used to think that all you can do with a moved-from object is do destruct it, but that would not be sufficient...

Effective C++11 update—Scott Meyers

This week, Scott Meyers posted a couple of updates on how C++11 is coming to one of the world's most-loved C++ book series -- Effective C++.

 

First, here's Scott's preamble about his approach to Effective C++11:

Effective C++11: Background

I've mentioned in some earlier posts that I plan to start writing a new book, Effective C++11.  The purpose of this post is to tell you a little bit about it. Lest there be confusion, let me emphasize that there is no book yet. If everything falls into place the way I hope it will, there will be a book about 10 months from now. If. I'm not making any promises. [...]

 

As a followup, Scott then posted an early draft list of candidate Items for Effective C++11 as part of this post:

Effective C++11: Content and Status

[...] At last year's C++ and Beyond, I gave a talk entitled "Initial Thoughts on Effective C++11." It had my usual guideline format. I also gave a talk on "Secrets of the C++11 Threading API," which consisted of observations about C++11's threading support. The material in those talks, combined with the feedback I got from giving them and mixed in with my experience explaining the idea of universal references, ultimately yielded the initial list of candiate Items for EC++11. The current snapshot of my vision for Effective C++11 is: [...]

At least a million developers are looking forward to your book, Scott!

No pressure.

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.