Thread: passing operators?

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838

    passing operators?

    i'm guessing this is impossible, but is it possible to point to an operator's function, i would ideally like to pass it as an argument.

    i would like to avoid having to pass a string literal to represent the operator and writing an if statement for each operator.

    is there any way one could achieve something akin to:

    Code:
    template<typename returnType,typename Arg>returnType func(Arg arg1,Arg arg2,operator op)
    {
    }
    matrix a;
    matrix b;
    func(a,b,a.*);
    there are probably errors in the above, it is meant only to illustrate the concept.

    it seems like there should be some way to create a function pointer to the operator, but i cannot get it...

    it would be nice if it were possible to do this for POD also.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Look in the <functional> header.
    It has lots of templates for things like:
    plus<>
    minus<>
    multiplies<>
    divides<>
    ...

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    templatizing the operation wasn't the issue; i need to store it!

    is there a way i can pass one of those as an arg?

    currently i have a series of if statements and corresponding member functions in the template class, but it would be nice if i could make it more eloquent.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about just...
    Code:
    template<typename T> void foo(T arg) { }
    int main()
    {
        obj myobj;
        foo(&myobj::operator+);
        // Or foo(&myobj.operator+);
    }
    Tried it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by m37h0d View Post
    templatizing the operation wasn't the issue; i need to store it!

    is there a way i can pass one of those as an arg?

    currently i have a series of if statements and corresponding member functions in the template class, but it would be nice if i could make it more eloquent.
    Most of those derive from binary_function<Type, Type, Type>, so you could probably store the function object as a binary_function<Type, Type, Type>& in your code...

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    @ Elysia; indeed, i had considered that, but what if T is an int or other POD type?

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by cpjust View Post
    Most of those derive from binary_function<Type, Type, Type>, so you could probably store the function object as a binary_function<Type, Type, Type>& in your code...
    i will look into that. thank you.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course, silly me, it must be:
    Code:
    template<typename T> void foo(T arg)
    { }
    
    class obj
    {
    public:
    	void operator + (int) {}
    };
    int main()
    {
    	obj myobj;
    	foo(&obj::operator+);
    }
    Works for me.
    Now you can call your operator from the template function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But that limits you to member +. It already doesn't work for a free operator +.

    You could just store a std::plus in a boost::function or a std::tr1::function.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by m37h0d View Post
    @ Elysia; indeed, i had considered that, but what if T is an int or other POD type?
    I don't see why you can't simply pass the address of an appropriate operator function. Even if it's free, you should be able to call it and pass the arguments.

    Code:
    template<typename T, typename T2> void foo(T arg, T2& arg2)
    { 
    	arg(arg2, 0);
    }
    
    class obj
    {
    public:
    	friend void operator + (const obj&, int);
    };
    
    void operator + (const obj&, int) {}
    
    int main()
    {
    	obj myobj;
    	foo(&operator+, myobj);
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I don't see why you can't simply pass the address of an appropriate operator function.
    Built-in operators aren't functions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, you mean built-in ones. I suppose this would only work on overloaded operators.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by Elysia View Post
    I don't see why you can't simply pass the address of an appropriate operator function. Even if it's free, you should be able to call it and pass the arguments.

    Code:
    template<typename T, typename T2> void foo(T arg, T2& arg2)
    { 
    	arg(arg2, 0);
    }
    
    class obj
    {
    public:
    	friend void operator + (const obj&, int);
    };
    
    void operator + (const obj&, int) {}
    
    int main()
    {
    	obj myobj;
    	foo(&operator+, myobj);
    }
    that can't possibly work. how could the template know that the type arg is a function?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The compiler deduces the type depending on what you pass.
    So what you pass IS the type, so you can use it AS that type.
    That's the power of templates.
    It does work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    perhaps i am being dense in not spotting a trivial error there, but that does not compile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing struct vs. reference
    By reakinator in forum C Programming
    Replies: 4
    Last Post: 06-14-2008, 10:11 PM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  3. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  4. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  5. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM