Articles & Books

Quick Q: Are there significant differences between boost::bind and std::bind? -- StackOverflow

A few minutes ago on SO:

Should I be seeing significant differences between std::bind and boost::bind?

I'm exploring the support for C++11 on the 4.7 branch of g++ (Ubuntu/Linaro 4.7.3-2ubuntu~12.04, to be specific) and I seem to be finding differences. In particular, if I comment out the #include of boost/bind.hpp and systematically replace occurrences of boost::bind with std::bind in the Boost ASIO async client example (taken from http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/http/client/async_client.cpp), the program no longer compiles. Any explanation for this?

 

Quick Q: What type does auto use for containers? -- StackOverflow

Quick A: std::initalizer_list. And did you know you can even do for(auto i: {1,2,3,4,5}) ?

What type does auto use for containers?

I can achieve identical output by using different containers in C++. For example . .

    std::array<int, 5> v = {1,2,3,4,5};
    for(auto i : v)
        std::cout << i << ", ";

or

    std::vector<int> v = {1,2,3,4,5};

or

    int v[] = {1,2,3,4,5};

etc.

So what container does auto use here?

    auto v = {1,2,3,4,5};
    for(auto i : v)
        std::cout << i << ", ";

Quick Q: Why does draft C++14 have both runtime-sized arrays and std::dynarray? -- StackOverflow

Several people have asked:

Why both runtime-sized arrays and std::dynarray in C++14?

Draft C++14 includes both runtime-sized arrays and the std::dynarray container. From what I can tell, the only real difference between the two is that std::dynarray has an STL interface (e.g., begin, end, size, etc.), while runtime-sized arrays do not. So why does C++14 need them both?

C++11 Regular Expression Library -- Brian Overland

New at InformIT:

C++11 Regular-Expression Library

by Brian Overland

From the article:

Regular expressions are of practical value in many programs, as they can aid with the task of lexical analysis—intelligently breaking up pieces of an input string—as well as tasks such as converting from one text-file format (such as HTML) to another.

I’ve found that when the C++11 regular expression library is explained in a straightforward, simple manner, it’s easy to use. This chapter doesn’t describe all the endless variations on the regex function-call syntax, but it does explain all the basic functionality: how to do just about anything you’d want to do.

Explicit Is Better Than Implicit -- K-ballo

tales-of-cpp.PNGTales of C++, Episode 5:

Explicit Is Better Than Implicit

The Zen of Python tell us that Explicit is better than Implicit. This is good advice for any and all languages, but what are the implications in the C++ lands? There is one particular C++ feature that relates directly to that advice; one important enough that grants on its own the introduction of a keyword. That feature is user-defined conversions and that keyword is explicit.

Quick Q: Am I capturing a member variable by value? -- StackOverflow

Quick A: No. You're capturing "this" by value, which is a pointer, which effectively captures members by reference.

From StackOverflow:

Lambda captures and member variables

I was watching Herb Sutter's talk at the C++ and Beyond 2012 conference on Concurrency and he talks about creating a non-blocking wrapper class, which he calls concurrent<T>, with C++11 functions.

His implementation is fairly simple (aside from needing a concurrent_queue such as that which exists in Microsoft's PPL):

template <class T>
class concurrent {
private:
    mutable T t;
    mutable concurrent_queue<std::function<void()>> q;
    bool done = false;
    std::thread thread;

public:
    concurrent( T t_ = T{} ) : t{t_}, thread{ [=]{ while( !done ) q.pop()(); }} {}

    ~concurrent() { q.push( [=]{ done = true; } ); thread.join(); }

    template <typename F>
    void operator()( F f ) const { q.push( [=]{ f(t); } ); }

};

This seems simple enough, however, I'm confused as to why he has captured the member variables done and q by value instead of by reference? My understanding is that if they are captured by value then they will be copied to the thread and thus when the queue is updated the worker thread will not receive the updates?

Have I misunderstood how lambda captures work with regards to class member variables? No one said anything in the comments to the video or during the talk so I am assuming that my understanding is faulty, in which case could someone please clarify?

Regular Expressions 101: Regex in C++11 -- Brian Overland

overland_brian_c.jpgNew at InformIT:

Regular Expressions 101: Regex in C++11

by Brian Overland

From the article:

... You don't really need to know how it works. In fact, you only need to know a few things:

  • The regular expression grammar used by the C++11 regex library functions
  • The relevant C++11 functions and how to call them

The first part of this article focuses on the default regex grammar used in C++11: the ECMAScript grammar. You'll generally want to use ECMAScript, because it provides powerful capabilities while being easy to use. For simplicity, I'll focus on this grammar. ...