boost::function



  • #include <iostream>
    #include <boost/function/function1.hpp>
    
    int foo( int a )
     {
         return a;
    }
    
     int main( )
     {
         boost::function1< int, int > f = foo;
    
         std::cout << f( 1 ) << std::endl;
    }
    
    ------ Build started: Project: Boost_Test, Configuration: Release Win32 ------
    Compiling...
    main.cpp
    Linking...
    Generating code
    Finished generating code
    Embedding manifest...
    Build log was saved at "file://c:\Boost_Test\Release\BuildLog.htm"
    Test2 - 0 error(s), 0 warning(s)
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
    


  • ricky schrieb:

    vielleicht so @Swordfish:

    #include <iostream>
    #include <boost/function/function1.hpp>
    
    int foo( int a )
    {
         return a;
    }
    
    int main( )
    {
         boost::function1< int, int> f;
    	 f = foo;
         std::cout << f( 1 ) << std::endl;
    }
    

    thx 👍

    @1310-Logik: Hä!? Selber Source:

    MSVC v7.1 (??) schrieb:

    main.cpp
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(19) : error C2039: 'function' : Ist kein Element von 'boost'
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(19) : error C2065: 'function' : nichtdeklarierter Bezeichner
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(19) : error C2144: Syntaxfehler : 'int' sollte auf ')' folgen
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(19) : error C2059: Syntaxfehler : ')'
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(23) : error C2065: 'f' : nichtdeklarierter Bezeichner
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(30) : error C2593: 'Operator <<' ist mehrdeutig

    Moment 'mal wie bekomm ich die Compilerversion 'raus?

    Greetz, Swordfish



  • bei mir so: Visual Studio 2005 Command Prompt starten dann folgendes eintippen:

    C:\>vbc
    Microsoft (R) Visual Basic Compiler version 8.0.50727.42
    for Microsoft (R) .NET Framework version 2.0.50727.42
    Copyright (c) Microsoft Corporation.  All rights reserved.
    

    cu



  • ricky schrieb:

    [...]
    Microsoft (R) Visual Basic Compiler version 8.0.50727.42
    [...]
    

    und was mach ich mit der Versionsnummer eines Visual Basic Compilers!?

    Greetz, Swordfish



  • Vielleicht dasselbe nochmal mit dem Visual C++ Compiler? 🙄



  • obs*g* naja ich glaub kaum das der vc compiler ne andere nummer hat;)
    ruf mal cl onder nmake auf...
    cu



  • @LordJaxom: kar, aber wie heißt der?

    Greetz, Swordfish



  • C:\Programme\Microsoft Visual Studio 8\VC\bin>nmake
    
    Microsoft (R) Program Maintenance Utility Version 8.00.50727.42
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    NMAKE : fatal error U1064: MAKEFILE not found and no target specified
    Stop.
    

    oder?



  • fu*k:

    Microsoft (R) Program Maintenance Utility, Version 7.00.9466
    

    Damit hat sich das Thema mit boost::function wohl erledigt. Kann mir jemand verraten, ob man in ein MSVS 2002 .net einen späteren Compiler einbinden kann?

    Greetz, Swordfish



  • hast du boost mit "-sTOOLS=vc-7_0" compiliert?

    cu



  • Ne, aber jetzt compilier ich 'grad mit "-sTOOLS=vc7".

    Greetz, Swordfish



  • und gehts besser?



  • Hallo,

    Mircosoftprodukte machen nur ärger :D.

    MFG winexec*



  • winexec* schrieb:

    Hallo,

    Mircosoftprodukte machen nur ärger :D.

    MFG winexec*

    Nur wenn man nicht weiß wie man damit umgeht. Und das gilt nicht nur für Microsoft Programme.

    Für mich sieht es einfach so aus als hätte da jemand unteranderem die Include Verzeichnisse falsch gesetzt (edit: Oder überhaupt nicht) und http://www.boost.org/more/getting_started.html nicht gelesen.

    BR
    Vinzenz



  • #include <iostream>
    #include <boost/lambda/lambda.hpp>
    #include <boost/function.hpp>
    #include <boost/bind.hpp>
    
    struct add
    {
    	float operator()(float x, float y) const
    	{
    		return x + y;
    	}
    };
    
    #define PRINT(expr) std::cout << "\t" #expr ": " << (expr) << std::endl;
    
    typedef boost::function<float(float,float)> function2d_t;
    typedef boost::function<float(float)> function1d_t;
    
    int main()
    {
    	using namespace boost;
    	namespace bl = boost::lambda; // use boost lambda to define unnamed functions
    	std::cout << "Adder example f(x,y) = x+y -> g(y) = 10+y" << std::endl;
    	function2d_t f = add(); // f(x,y) = x+y
    	function1d_t g = bind(f,10,_1); // g(y) = 10+y;
    
    	PRINT(f(10,20));
    	PRINT(g(20));
    
    	// classical division example using lambda functions
    	function2d_t div = bl::_1 / bl::_2;
    	function1d_t div_by_4 = bind(div,_1,4.0);
    
    	std::cout << "Division example div(x,y) : x/y" << std::endl;
    	PRINT(div(2,4));
    	PRINT(div_by_4(2));
    }
    

    wie funktioniert das denn? hmm...hat jemand ein besseres beispiel woran man erkennen kann wie boost::function mit boost::bind arbeiten?



  • vielleicht steh ich gerade wieder mal auf dem Schlauch, aber wo ist der Unterschied von

    ... 
    
        boost::function< int( int a ) > f; 
    
        if( a < 10 ) { 
    
            f = foo; 
    
        } else { 
    
            f = bar; 
        } 
    
        std::cout << f( a ) << std::endl;
    

    und

    if( a < 10)
        std::cout << foo(a) << std::endl;
    else
        std::cout << bar(a) << std::endl;
    

    Ich sehe da den Vorteil der Boost::function nicht...

    Gruß Paddy



  • Du stehst auf dem Schlauch. Das sind nur Beispiele, wie man es dynamisch machen kann. Was wäre zum Beispiel, wenn du kein std::endl sondern "asdf\n" anfügen wolltest? Nun stell dir das mal in einem großen switch vor, das gibt Spaß ;).
    Die eigentliche Verwendung von Funktionsobjekten sind aber mehr solche Sachen wie Callbacks.



  • Danke,

    Das mit den Callbacks, bzw. Zeigern auf Methoden, bzw. "generische Zeiger auf Methoden" war mir klar. Ich hatte lediglich den Vorteil in diesem Beispiel nicht gesehen...



  • evilissimo schrieb:

    winexec* schrieb:

    Hallo,

    Mircosoftprodukte machen nur ärger :D.

    MFG winexec*

    Nur wenn man nicht weiß wie man damit umgeht. Und das gilt nicht nur für Microsoft Programme.

    War das jetzt (auch) auf mich bezogen? 😡

    evilissimo schrieb:

    Für mich sieht es einfach so aus als hätte da jemand unteranderem die Include Verzeichnisse falsch gesetzt (edit: Oder überhaupt nicht) und http://www.boost.org/more/getting_started.html nicht gelesen.

    Doch, hab' ich (natürlich!). Bei einer Installation wie

    tree C:\programme\boost\include /a schrieb:

    C:\PROGRAMME\BOOST\INCLUDE
    \---boost-1_33_1
        \---boost
            +---algorithm
            |   \---string
            |       +---detail
            |       \---std
            +---archive
            |   +---detail
            |   +---impl
            |   \---iterators
            +---assign
            |   \---std
            +---bind
            +---compatibility
            |   \---cpp_c_headers
            +---config
            |   +---abi
            |   +---compiler
            |   +---platform
            |   \---stdlib
            +---date_time
            |   +---gregorian
            |   +---local_time
            |   \---posix_time
            +---detail
            +---dynamic_bitset
            +---filesystem
            +---format
            |   \---detail
            +---function
            |   \---detail
            +---functional
            |   +---detail
            |   \---hash
            +---graph
            |   \---detail
            +---integer
            +---io
            +---iostreams
            |   +---detail
            |   |   +---adapter
            |   |   +---broken_overload_resolution
            |   |   +---config
            |   |   +---streambuf
            |   |   \---vc6
            |   +---device
            |   \---filter
            +---iterator
            |   \---detail
            +---lambda
            |   \---detail
            +---logic
            +---math
            |   \---special_functions
            +---mpl
            |   +---aux_
            |   |   +---config
            |   |   +---preprocessed
            |   |   |   +---bcc
            |   |   |   +---bcc551
            |   |   |   +---dmc
            |   |   |   +---gcc
            |   |   |   +---msvc60
            |   |   |   +---msvc70
            |   |   |   +---mwcw
            |   |   |   +---no_ctps
            |   |   |   +---no_ttp
            |   |   |   \---plain
            |   |   +---preprocessor
            |   |   +---range_c
            |   |   \---test
            |   +---limits
            |   +---list
            |   |   \---aux_
            |   |       \---preprocessed
            |   |           \---plain
            |   +---map
            |   |   \---aux_
            |   |       \---preprocessed
            |   |           +---no_ctps
            |   |           +---plain
            |   |           \---typeof_based
            |   +---math
            |   +---multiset
            |   |   \---aux_
            |   +---set
            |   |   \---aux_
            |   |       \---preprocessed
            |   |           \---plain
            |   \---vector
            |       \---aux_
            |           \---preprocessed
            |               +---no_ctps
            |               +---plain
            |               \---typeof_based
            +---multi_array
            +---multi_index
            |   \---detail
            +---numeric
            |   +---conversion
            |   |   \---detail
            |   +---interval
            |   |   +---compare
            |   |   +---detail
            |   |   \---ext
            |   \---ublas
            |       \---detail
            +---optional
            +---parameter
            |   \---aux_
            +---pending
            |   \---detail
            +---pool
            |   \---detail
            +---preprocessor
            |   +---arithmetic
            |   |   \---detail
            |   +---array
            |   +---comparison
            |   +---config
            |   +---control
            |   |   \---detail
            |   |       +---dmc
            |   |       +---edg
            |   |       \---msvc
            |   +---debug
            |   +---detail
            |   |   \---dmc
            |   +---facilities
            |   +---iteration
            |   |   \---detail
            |   |       +---bounds
            |   |       \---iter
            |   +---list
            |   |   \---detail
            |   |       +---dmc
            |   |       \---edg
            |   +---logical
            |   +---punctuation
            |   +---repetition
            |   |   \---detail
            |   |       +---dmc
            |   |       +---edg
            |   |       \---msvc
            |   +---selection
            |   +---seq
            |   |   \---detail
            |   +---slot
            |   |   \---detail
            |   \---tuple
            +---program_options
            |   \---detail
            +---ptr_container
            |   \---detail
            +---python
            |   +---converter
            |   +---detail
            |   +---object
            |   \---suite
            |       \---indexing
            |           \---detail
            +---random
            |   \---detail
            +---range
            |   \---detail
            |       +---mfc
            |       \---vc6
            +---regex
            |   +---config
            |   +---pending
            |   \---v4
            +---serialization
            |   \---detail
            +---signals
            |   \---detail
            +---spirit
            |   +---actor
            |   +---attribute
            |   +---core
            |   |   +---composite
            |   |   |   \---impl
            |   |   +---impl
            |   |   +---non_terminal
            |   |   |   \---impl
            |   |   +---primitives
            |   |   |   \---impl
            |   |   \---scanner
            |   |       \---impl
            |   +---debug
            |   |   \---impl
            |   +---dynamic
            |   |   \---impl
            |   +---error_handling
            |   |   \---impl
            |   +---fusion
            |   |   +---algorithm
            |   |   |   \---detail
            |   |   +---detail
            |   |   +---iterator
            |   |   |   \---detail
            |   |   |       +---cons_iterator
            |   |   |       +---filter_view_iterator
            |   |   |       +---joint_view_iterator
            |   |   |       +---single_view_iterator
            |   |   |       +---transform_view_iterator
            |   |   |       +---tuple_iterator
            |   |   |       \---type_sequence_iterator
            |   |   \---sequence
            |   |       \---detail
            |   +---iterator
            |   |   \---impl
            |   +---meta
            |   |   \---impl
            |   +---phoenix
            |   +---symbols
            |   |   \---impl
            |   +---tree
            |   |   \---impl
            |   \---utility
            |       \---impl
            |           \---chset
            +---test
            |   +---detail
            |   +---impl
            |   +---included
            |   +---output
            |   \---utils
            |       +---basic_cstring
            |       +---iterator
            |       \---runtime
            |           +---cla
            |           |   +---detail
            |           |   \---iface
            |           +---env
            |           \---file
            +---thread
            |   \---detail
            +---tuple
            |   \---detail
            +---type_traits
            |   \---detail
            +---utility
            |   \---detail
            +---variant
            |   \---detail
            \---wave
                +---cpplexer
                |   \---re2clex
                +---grammars
                \---util
    

    und dem 'lib' Verzeichnis in C:\Programme\boost\lib\ ist ein
    Include Path von C:\Programme\Boost\include\boost-1_33_1\ und ein Lib Path von C:\Programme\Boost\lib wohl korrekt, oder?!

    Greetz, Swordfish



  • Das galt winexec weil er meint das MS Produkte nur ärger machen.

    Naja wenn du erst im nachhinein sagst, dass du das kompilierst dann frag ich mich ob du das vorher auch schon gesetzt hattest. Wie auch immer die Pfade sollten richtig sein.

    BR
    Vinzenz


Anmelden zum Antworten