LINQ++: An embeded DSL for C++

LINQ have been the new hotness in Microsoft’s .Net platform. Well, you can have the same syntactic sugar in C++ without writing a new compiler. I wrote a small library (just a short header file right now) that can do some interesting things. (source available at github) The following snippets from the companion unit test shows a few:

// Count the number of people older than 30
cout << from(guests)
        .where(&_1 ->* &Person::age > 30)
        .count()

// combine the people older than 30 with the person with name
// "joe" into one table.
DataSet<vector<Person> > results =
        insert(
                from(guests)
                .where(&_1 ->* &Person::age > 30))
        .into(
                from(guests)
                .where(&_1 ->* &Person::name == "joe"));

// select the age column from the previous table.
shared_ptr<vector<int> > ages = results
                                .select<int>(&_1 ->* &Person::age)
                                .get();

It should work with all STL-compatible sequence containers and requires the boost library. You can chain the clauses to form complicated queries.

I wrote it up on the shuttle from work to home. Hopefully I can find some time to polish it up and make it actually useful.

Comments 2

  1. Ferruccio wrote:

    I was thinking of doing something like this. Thanks for saving me the trouble!

    Posted 26 Oct 2008 at 5:41 am
  2. Fosma wrote:

    Great Work!

    Posted 01 Nov 2008 at 9:40 am

Trackbacks & Pingbacks 1

  1. From Giocando un po’ con LINQ++ (C++ e Boost Libs)… « JP’s Web Place on 22 Apr 2009 at 11:42 pm

    [...] sulla Rete, ho trovato un fantastico post di Hong Jiang intitolato “LINQ++: An embeded DSL for C++” ed ovviamente non potevo esimermi dal dargli un’occhiata da [...]

Post a Comment

Your email is never published nor shared.