standard C operator as parameter to template function - c++

This is a discussion on standard C operator as parameter to template function - c++ ; Dear newsgroup, I want to call a template function with a standard operator (+, /, -, * for double) as parameter. Here is a minimal example: template <typename T> T opIf(T val1, T val2, T (*op)(T,T) ) { if (val2 ...

+ Reply to Thread
Results 1 to 4 of 4

standard C operator as parameter to template function

  1. Default standard C operator as parameter to template function

    Dear newsgroup,

    I want to call a template function with a standard operator (+, /, -, * for
    double) as parameter. Here is a minimal example:

    template <typename T>
    T opIf(T val1, T val2, T (*op)(T,T) )
    {
    if (val2 != 0)
    return op(val1, val2);
    else
    return (T)0;
    }

    How can I call this function with e.g. the standard / operator?

    double res = opIf(10., 5., operator/);
    or similar does not work.

    Thanks a lot,
    Klaus

    --
    Please insert my name and a dot before my email address.


  2. Default Re: standard C operator as parameter to template function

    On Fri, 22 Jun 2007 11:31:11 +0200, Klaus Schneider wrote:

    > Dear newsgroup,
    >
    > I want to call a template function with a standard operator (+, /, -, * for
    > double) as parameter. Here is a minimal example:
    >
    > template <typename T>
    > T opIf(T val1, T val2, T (*op)(T,T) )
    > {
    > if (val2 != 0)
    > return op(val1, val2);
    > else
    > return (T)0;
    > }
    >
    > How can I call this function with e.g. the standard / operator?
    >
    > double res = opIf(10., 5., operator/);
    > or similar does not work.
    >
    > Thanks a lot,
    > Klaus
    >


    template <typename T, typename Functor>
    T opIf(T val1, T val2, Functor op )
    {
    if (val2 != 0)
    return op(val1, val2);
    else
    return (T)0;
    }

    #include <functional>
    #include <iostream>

    int main()
    {
    std::cout<<opIf(6,3,std::divides<int>())<<std::endl;
    std::cout<<opIf(6,3,std::multiplies<int>())<<std::endl;
    std::cout<<opIf(6,3,std:lus<int>())<<std::endl;
    std::cout<<opIf(6,3,std::minus<int>())<<std::endl;
    return 0;
    }

    --
    Obnoxious User

  3. Default Re: standard C operator as parameter to template function

    Klaus Schneider wrote:

    > Dear newsgroup,
    >
    > I want to call a template function with a standard operator (+, /, -, *
    > for double) as parameter. Here is a minimal example:
    >
    > template <typename T>
    > T opIf(T val1, T val2, T (*op)(T,T) )
    > {
    > if (val2 != 0)
    > return op(val1, val2);
    > else
    > return (T)0;
    > }
    >
    > How can I call this function with e.g. the standard / operator?
    >
    > double res = opIf(10., 5., operator/);
    > or similar does not work.


    What about:



    template < typename T, typename Op >
    T opIf ( T lhs, T rhs, Op op ) {
    if ( rhs != T() ) {
    return ( op( lhs, rhs ) );
    } else {
    return ( T() );
    }
    }

    #include <iostream>

    int main ( void ) {
    std::cout << opIf( 10.0, 5.0, std::divides<double>() ) << '\n';
    }



    Best

    Kai-Uwe Bux

  4. Default Re: standard C operator as parameter to template function

    On 6 22 , 5 31 , Klaus Schneider <schnei...@iup.uni-heidelberg.de>
    wrote:
    > Dear newsgroup,
    >
    > I want to call a template function with a standard operator (+, /, -, * for
    > double) as parameter. Here is a minimal example:
    >
    > template <typename T>
    > T opIf(T val1, T val2, T (*op)(T,T) )
    > {
    > if (val2 != 0)
    > return op(val1, val2);
    > else
    > return (T)0;
    >
    > }
    >
    > How can I call this function with e.g. the standard / operator?
    >
    > double res = opIf(10., 5., operator/);
    > or similar does not work.
    >
    > Thanks a lot,
    > Klaus
    >
    > --
    > Please insert my name and a dot before my email address.


    double res = opIf(10., 5., operator/);
    operator / is not a global operator..
    used std::divides<double> instead.


+ Reply to Thread

Similar Threads

  1. Replies: 5
    Last Post: 12-14-2007, 03:42 AM
  2. Replies: 3
    Last Post: 10-28-2007, 02:20 AM
  3. Replies: 3
    Last Post: 10-26-2007, 09:16 AM
  4. The CRTP pattern and member function pointer template parameter
    By Application Development in forum c++
    Replies: 4
    Last Post: 06-30-2007, 07:14 AM
  5. Can I pass an operator as a template parameter?
    By Application Development in forum c++
    Replies: 1
    Last Post: 04-08-2007, 04:44 PM