overload resolution

W

Wolfgang Jeltsch

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
 
W

Wolfgang Jeltsch

Wolfgang said:
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
 
F

Filipe Sousa

Wolfgang said:
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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top