Project Description

LINQ for C++ (cpplinq) is an extensible C++11 library of higher-order functions for range manipulation. cpplinq draws inspiration from LINQ for C#.

#include "cpplinq.hpp"

int computes_a_sum ()
{
    using namespace cpplinq;
    int ints[] = {3,1,4,1,5,9,2,6,5,4};

    auto result =    from_array (ints)
                  >> where ([](int i) {return i%2 ==0;})  // Keep only even numbers
                  >> sum ()                               // Sum remaining numbers
                  ;
    return result;
}

See Dr. Dobbs article by Gaston Hillar for an excellent introduction to cpplinq: http://www.drdobbs.com/cpp/linq-like-list-manipulation-in-c/240166882

Documentation

See documentation in order to get started and for more advanced topics such as creating your own range operators or range aggregators.

How to get it:

  1. Download the latest version of the source code (it's one self-contained file): https://cpplinq.codeplex.com/downloads/get/817114
  2. Add the header file to your C++ project
  3. include the header in the source files were you want to use the query operators (which are defined in the cpplinq namespace)
  4. In VC++ you have to define NOMINMAX in order to avoid including the min/max macros. If you use NuGet, this is automatically defined.

Supported compilers

  • Windows
    • Visual Studio 2010 (Toolkit v100) (Note: unit tests do not compile with this version)
    • Visual Studio 2012 (Toolkit v110)
    • Visual Studio 2013 (Toolkit v120)
    • MinGW (g++ v4.7.0)
      • v4.7.0 requires compiler option: -std=c++0x
  • Linux
    • g++ v4.7.0
      • v4.7.0 requires compiler option: -std=c++0x
    • g++ v4.8.0
      • v4.8.0 requires compiler option: -std=c++11
    • clang++ v3.1 (requires a patch to <chrono> to run unit tests, see cpplinq supported compilers)
    • clang++ v3.2
    • clang++ v3.4 (Unit tests are fixed for clang 3.4 in master)

NuGet Package for Visual Studio

A NuGet package for Visual Studio 2010 and newer is available here. You can directly install and update the package from Visual Studio, or run this command in a PowerShell window.
Install-Package cpplinq

When you use NuGet to automatically add the library, all the necessary settings (include folder, NOMINMAX pre-processor definition, etc.) are performed automatically.

Background

LINQ originally referred to the SQL-like syntax in C# and VB but it has over time changed its meaning to mean the way you manipulate lists using the higher-order functions provided by System.Linq. Working with lists using higher-order functions have been available for functional and SmallTalk developers since the 70s but has recently been popularized in mainstream languages such as C#, VB, JavaScript, Python, Ruby and so on. CppLinq is a library that tries to bring this style of programming to C++.

There are already several attempts at writing a LINQ library for C++, for instance:
and of course this library: cpplinq

(list taken from stackoverflow http://stackoverflow.com/questions/232222/is-there-a-linq-library-for-c)

Given all these alternatives why choose cpplinq?

In our opinion cpplinq is justified because:
  • cpplinq is elegant
    • The usage is elegant, the library is focused on performance, correctness and usage
  • cpplinq is extensible (operator >> can be overloaded, operator . can't)
  • cpplinq is a C++11 library
    • cpplinq doesn't need to rely on tricks or preprocessor to fake lambdas
  • cpplinq is easy to deploy (one file, no non-standard dependencies)
  • cpplinq is fast
    • We are constantly comparing cpplinq against hand-written loops.
  • cpplinq is tested on several compilers in order to stay standard compliant

The LINQ examples above has one or more of cpplinq's properties but only cpplinq fulfills all of them as far as we know. However, we encourage you to evaluate all different alternatives and pick the one that suites you best. It's our hope that you will pick cpplinq and enjoy it.

Last edited Jul 18 at 3:33 PM by marten_range, version 52