News

Quick Q: Is it idiomatic to make X objects immutable by making all X member data const? -- SO

Quick A: Yes.

Recently on SO:

Idiomatic way to declare C++ immutable classes

So I have some pretty extensive functional code where the main data type is immutable structs/classes. The way I have been declaring immutability is "practically immutable" by making member variables and any methods const.

struct RockSolid {
   const float x;
   const float y;
   float MakeHarderConcrete() const { return x + y; }
}

Is this actually the way "we should do it" in C++? Or is there a better way?

Cevelop Updated for the Eclipse Luna Service Release 1 -- Mirko Stocker

News about Cevelop, a C++ IDE for professional developers from the Institute for Software at HSR Hochschule für Technik:

Cevelop Updated for the Eclipse Luna Service Release 1

by Mirko Stocker

From the announcement:

We have rebased Cevelop on top of the latest Eclipse Luna Service Release SR1. The Eclipse CDT team used the chance to include some new features. For example, code completion can now show default arguments and gives you more information on template parameters... See the CDT 8.5 changelog for a comprehensive list of changes.

Quick Q: Which is more efficient, push_back(move(var)) or emplace_back(var)? -- StackOverflow

Quick A: 1. Those cases are not equivalent. 2. Emplace is more for when you don't already have a named object of the correct type...

Recently on SO:

Efficiency of C++11 push_back() with std::move versus emplace_back() for already constructed objects

In C++11 emplace_back() is generally preferred (in terms of efficiency) to push_back() as it allows in-place construction, but is this still the case when using push_back(std::move()) with an already-constructed object?

For instance, is emplace_back() still preferred in cases like the following?

std::string mystring("hello world");
std::vector<std::string> myvector;

myvector.emplace_back(mystring);
myvector.push_back(std::move(mystring));
// (of course assuming we don't care about using the value of mystring after)

Additionally, is there any benefit in the above example to instead doing:

myvector.emplace_back(std::move(mystring));

or is the move here entirely redundant, or has no effect?

Quick Q: How do I make an array of shared_ptrs to unrelated types? -- StackOverflow

Quick A: Try vector<shared_ptr<boost::any>>.

Recently on SO:

Array of shared pointers to different classes

I am new to c++11 programming and now I'm trying to figure out if it is possible to create an array of shared pointers to different types. For example, something like that:

vector<shared_ptr<**???**>> v;
v.push_back(shared_ptr<int>(new int));
v.push_back(shared_ptr<MyClass>(new MyClass()));

or any other way to pass shared_ptr without knowing its type.

CppCon videos now even more widely available via Channel 9

cppcon-c9.pngThis just in on CppCon.org:

Video Availability Increased

As of today, CppCon 2014 session videos are available on Channel 9 at: http://channel9.msdn.com/Events/CPP/C-PP-Con-2014

The videos were originally made available and are still available on the CppCon YouTube channel at: https://www.youtube.com/CppCon

After posting the videos on YouTube, we received requests for additional availability from countries where YouTube is not available. We’ve worked with Channel 9 to be an additional hosting site. Channel 9 is available in countries where YouTube is not, so this helps us in our goal of supporting C++ developers all over the world.

Channel 9 also supports downloading the sessions in a number of formats, including audio only, for offline use.

The CppCon 2014 conference videos feature are over 100 sessions of C++ content from many of the world’s best C++ experts, all professionally recorded and edited by Bash Films. We are grateful to both YouTube and Channel 9 for hosting our content.

Illinois Hosts International C++ Standards Conference -- Tom Moone

Following the recent ISO C++ meeting hosted by Riverbed and the University of Illinois at Urbana-Champaign, the University posted this summary of the event:

Illinois Hosts International C++ Standards Conference

By Tom Moone 

The article includes a link to Bjarne Stroustrup's talk to the faculty and students:

Illinois was selected as the location for this conference for two reasons. First, the location was capable of providing a week’s worth of meeting space for the 100 or so attendees in plenary and breakout sessions from 8 a.m. to 10 p.m. Second, the department, together with the local office of Riverbed Technology, offered to host the conference. “We are entirely dependent on being invited,” explained Sutter. “So we are very grateful to the department for accommodating us.”

On Monday evening of the conference, Stroustrup gave a talk as part of the CS Department’s Distinguished Lecture Series titled “C++ as a Modern Language.” In the talk he explained how to program in that language using type safety, resource safety, unmatched performance, and a terse notation. Enthusiasm for this lecture was literally overwhelming as the audience filled auditorium in the Siebel center to standing room only, and two overflow rooms were opened up to watch the talk with a live stream.

Functional Programming in C++ -- John Carmack

In case you missed it:

Functional Programming in C++

by John Carmack

From the article:

... My pragmatic summary: A large fraction of the flaws in software development are due to programmers not fully understanding all the possible states their code may execute in. In a multithreaded environment, the lack of understanding and the resulting problems are greatly amplified, almost to the point of panic if you are paying attention. Programming in a functional style makes the state presented to your code explicit, which makes it much easier to reason about, and, in a completely pure system, makes thread race conditions impossible.

I do believe that there is real value in pursuing functional programming, but it would be irresponsible to exhort everyone to abandon their C++ compilers and start coding in Lisp, Haskell, or, to be blunt, any other fringe language...

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++;});

Tiny Metaprogramming Library -- Eric Niebler

eric-niebler-toronto.PNGWe like to link to articles at all levels. This one's near the top of the scale, for those looking for a real challenge:

Tiny Metaprogramming Library

by Eric Niebler

As the intro says [emphasis ours]:

(Difficult-to-grok metaprogramming below. Not for the faint of heart.)

From the rest of the introduction:

At the recent Urbana-Champaign meeting of the C++ Standardization Committee, Bill Seymour presented his paper N4115: Searching for Types in Parameter Packs which, as its name suggests, describes a library facility for, uh, searching for a type in a parameter pack, among other things. It suggests a template called packer to hold a parameter pack:

// A class template that just holds a parameter pack:
template <class... T> struct packer { };

Many of you are probably already familiar with such a facility, but under a different name:

// A class template that is just a list of types:
template <class... T> struct typelist { };

It became clear in the discussion about N4115 that C++ needs a standard typelist template and some utilities for manipulating them. But what utilities, exactly? ...