Ownership and 'Memory -- Andy Balaam

andy.PNGA 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. ...

Quick Q: How do I use std::tie and std::ignore? -- StackOverflow

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::ignore from 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 is inserted set 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.

Quick Q: Why does shared_ptr of void work? -- StackOverflow

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? ]]

Quick Q: Does [=] capture all variables in scope? -- StackOverflow

Quick A: No.

A simple but important question:

C++11 lambda capture semantics

When I use [=] to indicate that I would like all local variables to be captured by value in a lambda, will that result in all local variables in the function being copied, or just all local variables that are used by the lambda?

So, for example, if I have:

vector<int> my_huge_vector(100000);
int my_measly_int;
some_function([=](int i){ return my_measly_int + i; });

Will my_huge_vector be copied, even though I don't use it in the lambda?

Quick Q: static constexpr variable vs. constexpr function? -- StackOverflow

With a nice Quick A by Morwenn that not only gives the right answer as of today, but is current with a feature voted into C++14 just two weeks ago that lets you drop the ()'s:

static constexpr variable vs. function

Is there a difference between declaring floating point constant as a static constexpr variable and a function as in example below, or is it just a matter of style?

class MY_PI
{
public:
    static constexpr float MY_PI_VAR = 3.14f;
    static constexpr float MY_PI_FUN() { return 3.14f; }
}

Functional Patterns in C++ -- Bartosz Milewski

bartosz-milewski-functional.pngIf you're familiar with functional language styles and you want an advanced look at how your favorite functional styles are supported in modern C++, with a dash of Haskell, check out these three videos by Bartosz Milewski:

Functional Patterns in C++ (slides)

by Bartosz Milewski

Part 1, Functors: First the introduction to some common functional patterns like Functor, which, surprisingly pops up everywhere. I'll show the example of a unique_ptr and a vector as Functors. Of course, this is only in preparation for asynchronous functors.

Part 2, Currying, Applicative: A little digression to Haskell and the Maybe functor and the explanation of currying. Then I'll show you the Applicative Functor pattern. This is, of course, in preparation for for the asynchronous applicative functor pattern.

Part 3, Asynchronous API, Monoid, Monad: The encapsulation of asynchronous API that doesn't lead to inversion of control and spaghetti code. Very natural example of a Monad Pattern.

Slides (parts 1-3)

Interestingly, Bartosz' talk ends with a plea for (essentially) future.then and a C#-style await... both of which are under active consideration in the C++ standards committee as part of a potential near-term C++ technical specification on concurrency and parallelism.

Advanced Developer Conference C++

adc.PNGThere's still time to go to:

Advanced Developers C++ 2013

May 7-8, 2013

Bad Aibling, Germany

Sessions are presented in either English or German. While the conference has many Windows-focused topics, a number of the sessions are of general interest to C++ developers.

Here are highlights from the Sessions page:

Trends and Future of C++ Standard and ISOCPP.org
Transactional Memory in C++
Michael Wong, IBM, and subgroup chair of ISO C++ SG5 (Transactional Memory)

Keynote: Building Modern Device Apps with C++ 
Building and Consuming Cloud Services with C++
Steve Teixeira, Microsoft

Warum wird Code so wie er ist? [Why does code get the way it does?]
Holger Kolb, DEVCOL

Einfacheres C++ mit C++11 [Simpler C++ with C++11]
Peter Sommerlad, FHO HSR Hochschule für Technik

Sicher sein oder sicher fühlen? -- Sicheren C++ Code schreiben [Be secure or feel secure? -- Writing secure C++ code]
Oliver Niehus, Microsoft

Performance-Optimierung für parallelen C++ Code auf Windows [Performance optimizing parallel C++ code on Windows]
Programmieren und Optimieren auf Xeon Phi [Programming and Optimizing for Xeon Phi]
Michael Steyer, Intel

Continue reading more sessions...

Trip Report: ISO C++ Spring 2013 Meeting, Part 3 -- Michael Wong

iconNoCommunityPhoto155.pngPart 3 of Michael Wong's trip report:

The view from C++ Standard meeting April 2013 Part 3

by Michael Wong

In this series that looks at C++14 content, we looked at features from Language and Library for C++14. Now we will look at Concurrency which is the other group that contributed features for C++14. In reality, some of the features from Language and Library also came from Concurrency. ...

An Idiot's Guide to C++ Templates -- Ajay Vijayvargiya

A lot of people appreciated the information in this pair of articles, which cover the noted topics:

An Idiot's Guide to C++ Templates -- Part 1

The Syntax Drama

Function Templates

  • Pointers, References and Arrays with Templates
  • Multiple Types with Function Templates
  • Function Template -- Template Function
  • Explicit Template Argument Specification
  • Default Arguments with Function Templates

Class Templates

  • Multiple Types with Class Templates
  • Non-type Template Arguments
  • Template Class as Argument to Class Template
  • Default Template Arguments with Class Templates
  • Class' Methods as Function Templates

An Idiot's Guide to C++ Templates -- Part 2

Requirements from the Underlying Type

  • Requirements: Function Templates
  • Requirements: Class Templates

Separation of Declaration and Implementation

  • Separating Class Implementation

Templates and Other Aspects of C++

  • Class Templates, Friends
  • Class Templates, Operator Overloading
  • Class Templates, Inheritance
  • Function Pointers and Callbacks
  • Templates and Virtual Functions
  • Templates and Macros
  • Function Overloading

STL - An Introduction

Templates and Library Development

Explicit Instantiation

C++ on the Web: Run Your Big 3D Game in the Browser! -- Andre Weissflog

cpp-web.PNGBuilding C++ to target Javascript (e.g., asm.js) and execute C++ in the browser is becoming quite the popular indoor sport. Here's a current presentation and experience report:

C++ on the Web: Run your big 3D Game in the browser! (slides)

by Andre Weissflog
Head of Development, Berlin
Bigpoint GmbH

My presentation about porting large C/C++ code bases to the browser (emscripten, flascc, Google Native Client)

From the Wrap-up slide:

  • You can run big C/C++ code bases ("a million lines of code") in the browser.
  • Javascript is already fast enough for many types of games.
  • Massive performance improvements happening right now (better code generation, JS engines better at running generated code, asm.js...)
  • ...