Thread: ambiguous overload with derived classes?

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Question ambiguous overload with derived classes?

    basically, I have something like this:

    Code:
     
    struct foo
    {      };
     
    void
    bar( foo const & lhs, foo const & rhs )
    {      }
     
    template < typename Lhs >
    void
    bar( Lhs const & lhs, foo const & rhs )
    {      }
     
    template < typename Rhs >
    void
    bar( foo const & lhs, Rhs const & rhs )
    {      }
     
    struct baz : foo
    {      };
     
    int
    main( void )
    {
          bar( foo( ), foo( ) ); // fine
          bar( 1024, foo( ) ); // fine
          bar( foo( ), 1024 ); // fine
          bar( baz( ), baz( ) ); // error: ambiguous overload
          bar( 1024, baz( ) ); // fine
          bar( baz( ), 1024 ); // fine
     
    }
    it's really not clear to me why the compiler doesn't choose bar( foo const &, foo const & ) as the closest match. is there a workaround for this, or will I have to wait for c++0x to implement a 'where' clause?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    bar( baz() , baz() ) models both of the templates and the regular bar() function.
    I can't see a way to explicitly specify which version of bar() you want, but one solution is to make just one template, but with default arguments. Since function templates can't have default arguments, you would have to use a function object, which may be stylistically distasteful:

    Code:
    template<typename Lhs = const struct foo&, typename Rhs = const struct foo&>
    struct bar
    {
       void operator() (Lhs arg1, Rhs arg2) { /* ... */ }
    };
    
    /* ... */
    bar<>()( baz(), baz() );
    /* Implicit conversion of 'const struct baz' to 'const struct foo&' */
    It's likely that there's a much simpler solution.
    Last edited by rudyman; 05-22-2008 at 07:25 PM.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    well, among other things complicating the matter, 'bar' is actually an overloaded operator, so that won't work. but thanks for the suggestion anyway.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. storing derived classes in a stl container
    By *DEAD* in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2008, 07:50 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 04:07 PM