My first real lambda expression in C++0x

> Coding, hacking, computer graphics, game dev, and such...
User avatar
fips
Site Admin
Posts: 170
Joined: Wed Nov 12, 2008 9:49 pm
Location: Prague
Contact:

My first real lambda expression in C++0x

Post by fips »

Finally, a sensible way to express Predicates. Lambda rocks! No Boost required any more!

Code: Select all

int ModulePool::FindByFile(const String &file) const
{
    const Modules::const_iterator it = std::find_if(
     modules_.begin(), modules_.end(),
     [&file](const ModuleInfo &mi) { return mi.file_ == file; }
    );
    return it != modules_.end() ? std::distance(modules_.begin(), it) : -1;
}
User avatar
fips
Site Admin
Posts: 170
Joined: Wed Nov 12, 2008 9:49 pm
Location: Prague
Contact:

Re: My first real Lambda expression in C++0x

Post by fips »

Another Lambda function I've just written - It initializes a static variable vertexDeclaration with a vector of VertexElements constructed from an array. (It would be even easier to use std::initializer_list for this, but it's not supported in Visual C++ 2010 at this moment.)

Code: Select all

const VertexDeclaration VertexPositionColor::vertexDeclaration(
[]() -> VertexElementVector
{
    const VertexElement elems[] = {
     VertexElement(0, VEF_Vector3, VEU_Position, 0),
     VertexElement(0, VEF_Color, VEU_Color, 0),
    };

    return VertexElementVector(
     &elems[0],
     &elems[sizeof(elems) / sizeof(elems[0])]
    );
}());
Post Reply