Page 1 of 1

My first real lambda expression in C++0x

Posted: Fri Jul 16, 2010 11:21 pm
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;
}

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

Posted: Sun Jul 25, 2010 1:24 pm
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])]
    );
}());