Author: Eric Niebler

  • Meet Me at Meeting C++

    Jens Weller was foolish enough to give me the prime keynote slot at this year’s Meeting C++ in Düsseldorf. For 2 hours in November, I’ll be in TOTAL CONTROL of the newest and fastest growing C++ conference in Europe. Mwahaha! In all seriousness, I’m deeply honored and eagerly looking forward to Meeting C++. I mean, just look at the list of talks. Makes you want to go, right?

    Here’s what I’ll be talking about:

    C++11 and No-Compromise Library Design

    If you’re writing reusable code and you’re not taking full advantage of C++11’s new features, stop what you’re doing! The rules have changed, from the bottom — like parameter-passing — to the top — like library versioning. Code written with C++11 can be more modular and composable than ever, with easier and safer-to-use interfaces, with better template error messages, and with less boilerplate. It’s library design without the compromises.

    I’ll be giving examples from the redesigned Boost.Proto, an advanced library for building expression template-based libraries, but the lessons are broadly applicable to any code that is intended to be reused. The usual suspects like move semantics, and variadics will be covered, but also some lesser known players that change the landscape in surprising ways, including the ONE tiny C++11 feature that caused the most grief and provided the most unique solution…

    See you in November!

  • Optimizing Expressions with Boost.Proto

    Finally, a new “Expressive C++” article. I’m pretty excited because I’ve covered enough of Proto’s basics to get to talk about the good stuff: how to manipulate expressions using grammars and transforms. This is potent stuff. In this article, I show how to use Proto to completely eliminate the abstraction overhead in a simple linear algebra library. Without giving up the accepted domain syntax. And without a pile of nasty template spaghetti code. Enjoy.

    Expressive C++: Expression Optimization

  • Expressive C++: Expressions and Domains

    My latest article about EDSLs and Boost.Proto is up at Cpp-Next. This one completes a little lambda library in about 30 lines of C++. I also ruminate on the many layers of understandings about expressions in C++. Swing on over, have a look, leave a comment!

    Expressive C++: A Lambda Library in 30 Lines (Part 2 of 2)

  • Expressive C++ Continues

    The latest in my series of posts on embedded Domain-Specific Languages in C++ continues. This one sketches the outline of a simple lambda library using Boost.Proto.

    Expressive C++: A Lambda Library in 30 Lines (Part 1 of 2)

    Enjoy!

  • Xpressive To Get Multi-Capture

    For Boost.Xpressive, multi-capture (the ability of a capturing group to remember all the times matches, instead of only the last) is an oft-requested feature. Since it could be faked with Xpressive’s implementation of named regexes, so adding multi-capture has never been high on my priority list. Well, it looks like someone went ahead and implemented it for me:

    From: "Erik Rydgren" 
    Date: Sun, 3 Oct 2010 02:52:23 +0200
    Local: Sat, Oct 2 2010 5:52 pm
    Subject: [boost] [xpressive] Support for multi-capture and
    balancing groups
    
    Hi!
    
    My company have been using pcre for a long while but there has
    been gripes about it only returning the last matched value
    from a capture group. Because of this I have been searching
    for a C++ regex engine that can handle the same stuff as the
    .NET implementation can do, to no avail. During my searches
    I've stumbled on several forum threads where others have been
    searching for the same thing but it doesn't seem to exist a
    regular expression library in C/C++ that handles both named
    captures and multicapture.
    
    Found boost.xpressive and it had almost everything we need.
    It's open source, fast, got flexible api and named captures.
    But alas, just as all other C and C++ based implementations
    I have found, it lacked multiple captures.
    
    So, I added it. On top of that I added support for balancing
    groups
    (http://blog.stevenlevithan.com/archives/balancing-groups).
    But the syntax for the pop capture and capture conditional is
    slightly different then the .NET version to better fit
    xpressive.
    
    Syntax for pop capture:
      dynamic: (?Pstuff)
      static: (name -= stuff)
    
    Syntax for capture conditional:
      dynamic: (?P(name)stuff)
      static: (name &= stuff)
    
    There is no support for the (?stuff) construct.
    
    All captures made by a group is stored in sub_match::captures
    which is a vector of sub_match_capture objects. A
    sub_match_capture behaves like a stripped down sub_match. It
    can be put in an ostream and has a length and helper function
    for returning a string.
    
    The changes are in the vault and can be found here:
    http://tinyurl.com/3aak7mp
    
    It can be unpacked against trunk from 2010-10-02 or the 1.44.0
    release. I've run the dynamic regression tests without errors
    and I have added some tests for the new functionality. The
    code it only tested on Visual Studio 2010 since I don't have
    access to any other compiler.
    
    Please give feedback on my changes since I would love to see
    them in an official release. Thanks in advance.
    
    Regards,    
    Erik
    

    Very, very cool. I love open source!

  • Boost.Xpressive FTW

    This message recently came over the boost-users mailing list:

    Subject: [Boost-users] best tool in Boost for (massive) string
             replacement?
    From: alfC (alfredo.correa_at_[hidden])
    Date: 2010-09-23 18:11:20 
    
    With all the tools available in Boost and coming from a
    different backgroup is hard for me to choose what is the best
    tool in Boost to do a massive string replacement.
    
    The problem I have is the following, I have a map of replaced
    and replacing strings
    
    std::map<std::string, std::string> rep;
    rep["alpha"] = "a";
    rep["beta"] = "b";
    ...
    
    let's say about 100 of these. And I have an input/output file
    (few thousand lines) were I would like to do all this
    replacements. What is the best tool in boost to do this,
    Spirit, Regex, tokenizer, StringAlgorithm?
    
    My only approach so far is Regex and the implementation is
    very crude. I read the file line by line and do a loop over
    the replacement keys for each line. It is not even
    exploiting the fact that I have a map of replacements
    (compared to an array of replacements). It seems very slow.
    
    (Yes, it is like a 'sed' unix command replacement but with
    hundreds of replacement strings)
    
    Thank you,
    Alfredo
    

    Here was my answer:

    Subject: Re: [Boost-users] best tool in Boost for (massive)
             string replacement?
    From: Eric Niebler (eric_at_[hidden])
    Date: 2010-09-25 21:04:56 
    
    None of the above. Use Boost.Xpressive. The complete solution
    is below:
    
        #include <map>
        #include <string>
        #include <iostream>
        #include <boost/xpressive/xpressive_static.hpp>
        #include <boost/xpressive/regex_actions.hpp>
        using namespace std;
        using namespace boost::xpressive;
    
        int main()
        {
            std::map<std::string, std::string> rep;
            rep["alpha"] = "a";
            rep["beta"] = "b";
            rep["gamma"] = "g";
            rep["delta"] = "d";
    
            local<std::string const *> pstr;
            sregex const rx = (a1 = rep)[pstr = &a1];
    
            std::string str("alpha beta gamma delta");
            std::cout << regex_replace(str, rx, *pstr)
                      << std::endl;
        }
    
    The regex (a1 = rep) takes the keys in the rep map and builds
    a search trie (http://en.wikipedia.org/wiki/Trie) out of them.
    When the trie matches, the attribute a1 receives the value
    associated with the matching key. The semantic action
    [pstr = &a1] assigns the address of the value to the local
    variable pstr.
    
    The call to regex_replace uses the lambda expression *pstr as
    the replacement.
    

    With Boost.Xpressive, the solution is short and efficient. If we instead call the version of regex_replace that writes to an output iterator, we can do the replacement with only one or two dynamic allocations (used internally by xpressive for holding backreferences and such). Another victory for embedded DSLs.