tags:

views:

138

answers:

3

It is possible to get functionality similar to .NET's LINQ in C++? Would this require language extensions or could it be done using some very clever macros? Or even through a tool like Qt's moc (meta-object compiler)? Are there any existing LINQ implementations for C++ and if so, what are they?

+5  A: 

Check CLinq (here and here) or Linq++ (here). Also try p-stade (here); however, it uses the STL and it doesn't have the same interface as LINQ, but it's pretty complete.

Yassin
CLinq is for C++/CLI, and C++/CLI ≠ C++.
missingfaktor
@Missing Faktor: The techniques that I used in the implementation of CLinq would work in plain C++ too. It is in C++/CLI only because I wanted to use the existing .NET API (LINQ libraries), but that could be reimplemented in C++. CLinq is pretty much implemented just using templates (not generics :-)) and usual operator overloading.
Tomas Petricek
@Tomas: Good to know :)
missingfaktor
A: 

Looking at the way C++ committee works, a feature like LINQ is at least 50 years away from us.

It's sad, but true. :-(

Aai Ghatli
This is not an answer.
GMan
The question is whether a language extension is required, not how long such an extension might take to be standarized.
David Rodríguez - dribeas
Though correct, this post doesn't contribute anything worthwhile to the discussion, and is almost offtopic.
one-zero-zero-one
+1  A: 

It is possible to get functionality similar to .NET's LINQ in C++? Would this require language extensions or could it be done using some very clever macros?

C++ macros aren't powerful enough to implement something as complex as LINQ.

To implement LINQ-like system in the form of library, the language needs:

  • Good embedded-DSL capabilities.
  • Lazy evaluation
  • Persistent collections
  • Lambda expressions

Embedded DSLs in C++ look very ugly thanks to the rigorous syntax and semantics of the language (for example, look at Boost.Spirit and then look at an equivalent library from a DSL-friendly language like Haskell). You can get lazy evaluation via boost::phoenix. There is no persistent collections library available for C++ (apart from FC++, which is pretty incomplete). Lambda Expressions are coming to C++ in the next standard of the language.

Even if someday someone manages to create a LINQ-like system for C++ using above-mentioned ingredients, that system won't be as good as LINQ in .NET. So yes, it is possible, but not very practical. :)

Or even through a tool like Qt's moc (meta-object compiler)?

This is very much possible. But then it won't still really be C++, will it? ;)

Are there any existing LINQ implementations for C++ and if so, what are they?

A few attempts have been made in this direction (as pointed out by other gentleman here). None of them comes close to the "real" LINQ, but they're still worth having a look at. :)

EDIT:
Apparently I was wrong about the "practical" bit. Look at the p-stade link in Yassin's answer, which is a great example of what can be achieved with clever use of powerful C++ abstractions. :-)

one-zero-zero-one
I would disagree with this in so far as you could get by with a builder pattern: `mybuilder.select("Trees").from("Sprites").where("Height > 4");`
wheaties
@wheaties: And what would you do with those strings? To do anything useful with those strings, you'd need reflection - something that C++ doesn't have.
one-zero-zero-one
@Cheryl Vincent I think that would work pretty darned well for LINQ on SQL.
wheaties
@wheaties: ...and what about LINQ on collections?
one-zero-zero-one
You mean it's actually not worth the trouble to try and implement a LINQ approach to a problem in C++, because C++ isn't a language with LINQ? Wow. A new approach might be to see what C++ can do and explore that direction, taking advantage of what it *does* have to offer.
GMan
@GMan: Have a look at p-stade link in Yassin's answer. Looks quite nice and useful.
one-zero-zero-one