overload resolution

Discussion in 'C++' started by Wolfgang Jeltsch, Aug 31, 2003.

  1. Hello,

    consider the following code:
    #include <iostream.h>

    template < typename T >
    int f( T v ) {
    return 0;
    }

    int f( int i ) {
    return i;
    }

    int main() {
    cout << f( 1 ) << '\n';

    return 0;
    }

    Is this correct and guaranteed to output 1? Or is there a conflict between
    the template f which can be instantiated to
    int f< int >( int )
    and the function f which is declared as
    int f( int )?

    Wolfgang
    Wolfgang Jeltsch, Aug 31, 2003
    #1
    1. Advertising

  2. Wolfgang Jeltsch wrote:

    > Hello,
    >
    > consider the following code:
    > #include <iostream.h>
    >
    > template < typename T >
    > int f( T v ) {
    > return 0;
    > }
    >
    > int f( int i ) {
    > return i;
    > }
    >
    > int main() {
    > cout << f( 1 ) << '\n';
    >
    > return 0;
    > }
    >
    > Is this correct and guaranteed to output 1? Or is there a conflict between
    > the template f which can be instantiated to
    > int f< int >( int )
    > and the function f which is declared as
    > int f( int )?
    >
    > Wolfgang


    Additional question: How does overload resolution work if we take
    constructors instead of ordinary functions:
    template < typename T >
    class C {
    private:
    T v;

    public:
    template < typename TT >
    C( const C< TT > &source );
    C( const C &source );
    };

    I think that for implicit copy operations the explicitely defined copy
    constructor is used instead of a template instance with T equal to TT. But
    what is done if I initialize explicitely or call constructors explicitely?
    Is there a conflict if T and TT are equal?

    Thanks in advance!

    Wolfgang
    Wolfgang Jeltsch, Aug 31, 2003
    #2
    1. Advertising

  3. Wolfgang Jeltsch

    Filipe Sousa Guest

    Wolfgang Jeltsch wrote:

    > Hello,
    >
    > consider the following code:
    > #include <iostream.h>
    >
    > template < typename T >
    > int f( T v ) {
    > return 0;
    > }
    >
    > int f( int i ) {
    > return i;
    > }
    >
    > int main() {
    > cout << f( 1 ) << '\n';
    >
    > return 0;
    > }
    >
    > Is this correct and guaranteed to output 1? Or is there a conflict between
    > the template f which can be instantiated to
    > int f< int >( int )
    > and the function f which is declared as
    > int f( int )?
    >
    > Wolfgang


    the result is garanteed to be 1 because int f( int ) is more especific than
    int f<int>(int)
    Filipe Sousa, Aug 31, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eph0nk

    Error: Overload resolution failed

    Eph0nk, Oct 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,859
    Eph0nk
    Oct 23, 2003
  2. =?Utf-8?B?Q2FpbGlu?=
    Replies:
    0
    Views:
    1,590
    =?Utf-8?B?Q2FpbGlu?=
    Sep 9, 2005
  3. Alexander Stippler
    Replies:
    6
    Views:
    427
    Rolf Magnus
    Oct 30, 2003
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    368
    Nick Hounsome
    Jan 25, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    248
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page