Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

lambda expressions


SourceForge.net Logo     CTTL on    
    SourceForge    
    Download    
    Latest    
    Documentation    
    Index    
    Library    
    News    
    CVS    
    Repository    
   Other    
   Links    

lambda expression

non-reusable code in-line.

overloaded assignment

#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

template< typename LambdaT >
void f( LambdaT lambda_ )
{
    lambda_.evaluate(); // delayed evaluation
}

void g()
{
    int var = 0;
    f( scalar( &var ) = 5 ); // delayed expression "var = 5"
    assert( var == 5 );
}

invoke member function with no arguments

#define CTTL_TRACE_EVERYTHING

#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

struct handler
{
    // void member function with no arguments 
          
    void vmf()
    {
    }

    // constant void member function with no arguments
    void vmfc() const
    {
    }
};

int main(int argc, char* argv[])
{
    lambda< int >::scalar sresult;
    handler hand;

    // calls to member function with no arguments:
    std::mem_fun( &handler::vmf )( &hand );
    std::mem_fun( &handler::vmfc )( &hand );
    (
        sresult = *scalar(
            CTTL_MEMBER_ACTION_NOARG(
                &hand,
                std::mem_fun( &handler::vmf ) // or vmfc
            )
        ),
        CTTL_LAMBDA_ASSERT( sresult == 1 )

    ).evaluate();

    return 0;
}

call member function with no arguments

#define CTTL_TRACE_EVERYTHING

#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

struct handler
{
    // member function with no arguments
    int mf()
    {
        return 2;
    }

    // constant member function with no arguments
    int mfc() const
    {
        return 3;
    }
};

int main(int argc, char* argv[])
{
    int result = 0;
    lambda< int >::scalar_reference sresult( &result );
    handler hand;

    // calls to member function with no arguments:
    result = std::mem_fun( &handler::mf )( &hand );
    result = std::mem_fun( &handler::mfc )( &hand );
    (
        sresult = *scalar(
            CTTL_MEMBER_ACTION_NOARG(
                &hand,
                std::mem_fun( &handler::mf ) // or mfc
            )
        ),
        CTTL_LAMBDA_ASSERT( sresult == 2 )

    ).evaluate();

    return 0;
}

call member function with one argument

#define CTTL_TRACE_EVERYTHING

#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

struct handler
{
    // constant member function
    int mfv( int x_ ) const
    {
        return x_;
    }

    // member function with mutable argument
    int mfr( int& x_ )
    {
        return ++x_;
    }

    // constant member function with constant argument
    int const& mfrc( int const& x_ ) const
    {
        return x_;
    }
};

int main(int argc, char* argv[])
{
    int result = 0;
    lambda< int >::scalar_reference sresult( &result );
    lambda< int >::scalar sargument( 0 );
    handler hand;

    // calls to member function with one argument:
    result = std::mem_fun( &handler::mfv )( &hand, sargument.top() );
    result = std::mem_fun( &handler::mfr )( &hand, sargument.top() );
    result = std::mem_fun( &handler::mfrc )( &hand, sargument.top() );
    (
        sresult = *scalar(
            CTTL_MEMBER_ACTION(
                &hand,
                std::mem_fun( &handler::mfv ),
                sargument.top()
            )
        ),
        CTTL_LAMBDA_ASSERT( sresult == 1 ),

        sresult = *scalar(
            CTTL_MEMBER_ACTION(
                &hand,
                std::mem_fun( &handler::mfr ),
                &sargument.top()
            )
        ),
        CTTL_LAMBDA_ASSERT( sresult == 2 ),

        sresult = *scalar(
            CTTL_MEMBER_ACTION(
                &hand,
                std::mem_fun( &handler::mfrc ),
                sargument.top()
            )
        ),
        CTTL_LAMBDA_ASSERT( sresult == 2 )

    ).evaluate();

    return 0;
}

invoke void member function with one argument

#define CTTL_TRACE_EVERYTHING

#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

struct handler
{
    // constant void member function
    void mfc( int ) const
    {
    }

    // void member function with mutable argument
    void mf( int& x_ )
    {
        ++x_;
    }
};

int main(int argc, char* argv[])
{
    int result = 0;
    lambda< int >::scalar_reference sresult( &result );
    lambda< int >::scalar sargument( 0 );
    handler hand;

    // calls to member function with one argument:
    std::mem_fun( &handler::mfc )( &hand, sargument.top() );
    std::mem_fun( &handler::mf )( &hand, sargument.top() );
    (
        sresult = *scalar(
            CTTL_MEMBER_ACTION(
                &hand,
                std::mem_fun( &handler::mfc ),
                sargument.top()
            )
        ),
        CTTL_LAMBDA_ASSERT( sargument == 1 ),

        sresult = *scalar(
            CTTL_MEMBER_ACTION(
                &hand,
                std::mem_fun( &handler::mf ),
                &sargument.top()
            )
        ),
        CTTL_LAMBDA_ASSERT( sargument == 2 )

    ).evaluate();

    return 0;
}



Copyright © 1997-2006 Igor Kholodov mailto:cttl@users.sourceforge.net.

Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.


Generated on Thu Nov 2 17:48:53 2006 for CTTL Lambda Expression by  doxygen 1.3.9.1