1. 4FIPS
  2. PHOTOS
  3. VIDEOS
  4. CODE
  5. FORUMS
  6. ABOUT

4FipS.com Forums

Forums dedicated to the projects hosted at 4FipS.com

Skip to content

My first real lambda expression in C++0x

> About coding, hacking, photography, and such...

My first real lambda expression in C++0x

Postby FipS on Fri Jul 16, 2010 11:21 pm

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: 159
Joined: Wed Nov 12, 2008 9:49 pm
Location: Prague

My first real lambda expression in C++0x

Sponsor

Sponsor
 

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

Postby FipS on Sun Jul 25, 2010 1:24 pm

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])]
    );
}());
User avatar
FipS
Site Admin
 
Posts: 159
Joined: Wed Nov 12, 2008 9:49 pm
Location: Prague


Return to 4FipS.com General Discussion & Blog

Who is online

Users browsing this forum: No registered users and 3 guests

cron