passing operators?

This is a discussion on passing operators? within the C++ Programming forums, part of the General Programming Boards category; How about this? Code: template<typename T, typename T2> void foo(T arg, T2& arg2) { arg(arg2, 0); } void operator + ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    How about this?
    Code:
    template<typename T, typename T2> void foo(T arg, T2& arg2)
    { 
    	arg(arg2, 0);
    }
    
    void operator + (const obj&, int);
    
    class obj
    {
    public:
    	friend void operator + (const obj&, int);
    };
    
    void operator + (const obj&, int) {}
    
    int main()
    {
    	obj myobj;
    	foo(&operator+, myobj);
    }
    I know GCC is a little more strict about things, but I don't want to copy and compile it right now.
    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.

  2. #17
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by Elysia View Post
    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.
    that's tantamount to a kind of implied specialization, though, isn't it?

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    Not in this case.

    See, the compiler has two conflicting requirements here. It has to deduce the template argument based on what is passed, so it needs a definite type as the argument. However, the address of a function is the one situation where the compiler can have a vague type on the right hand side and use the definite type of the left hand side for deduction - overload resolution, that is. Since neither the left nor the right side is definite, you get an error.

    You need an explicit cast in this case:
    Code:
    foo(static_cast<void (*)(const obj&, int)>(&operator +), myobj);
    Meh. Easier to just pass a std::plus as a boost::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

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    Code:
    template<typename T, typename T2> void foo(T arg, T2& arg2)
    {
    	arg(arg2, 0);
    }
    
    class obj;
    void operator + (const obj&, int);
    
    class obj
    {
    public:
    	friend void operator + (const obj&, int);
    };
    
    void operator + (const obj&, int) {}
    
    int main()
    {
    	obj myobj;
    	foo(&operator+, myobj);
    }
    This works on GCC.
    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. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Add an include for <string>, import the entire std namespace, and it stops working. That's a little too brittle for my taste.
    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

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    This is what sometimes is dangerous about free operators. Best put them inside a namespace to avoid conflicts.
    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.

  7. #22
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    wtf...

    Code:
    //---------------------------------------------------------------------------
    #include <functional>
    #pragma hdrstop
    
    int main()
    {
            greater<int>();
            return 0;
    }
    results in "undefined symbol " for any of the function objects defined in functional.h...

    am i being dense here?

  8. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes. But do you really want to put every overload of an operator in its own namespace? That means putting all the classes in these separate namespaces too, or the ADL won't find the operator.

    m37h0d: How about explicitly qualifying std?
    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

  9. #24
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by m37h0d View Post
    wtf...

    Code:
    //---------------------------------------------------------------------------
    #include <functional>
    #pragma hdrstop
    
    int main()
    {
            std::greater<int>();
            return 0;
    }
    results in "undefined symbol " for any of the function objects defined in functional.h...

    am i being dense here?
    Add std::

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    Quote Originally Posted by CornedBee View Post
    Yes. But do you really want to put every overload of an operator in its own namespace? That means putting all the classes in these separate namespaces too, or the ADL won't find the operator.
    Sticks and stones, I guess. Use whichever suits your purpose best.
    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. #26
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by CornedBee View Post
    Yes. But do you really want to put every overload of an operator in its own namespace? That means putting all the classes in these separate namespaces too, or the ADL won't find the operator.

    m37h0d: How about explicitly qualifying std?
    doh.

  12. #27
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Code:
    boost::bind(&SomeClass::operator+, _1, _2)
    This produces a binary functor which when invoked executes SomeClass's operator+ on its operands.

    It doesn't work if more than one overload of operator+() is available -- if that's the case you'll need to write your own functor to bounce through.

Page 2 of 2 FirstFirst 12
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, 06: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, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21