473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

overload resolution

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
Jul 19 '05 #1
2 1592
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
Jul 19 '05 #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


the result is garanteed to be 1 because int f( int ) is more especific than
int f<int>(int)

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
1
by: M Jared Finder | last post by:
What are the rules for method overload resolution when there are functions with a params argument? No mention of params is made in section 7.4.2 of the C# specification on MSDN (titled "Overload...
13
by: Vladimir Granitsky | last post by:
Hi guys, Please, look at the code below and try to step into it. The compiled code calls the loosely typed method public void Method1(object o) !?!? Am I right that C# compiler does wrong...
0
by: Eph0nk | last post by:
Hi, I get an overload resolution failed error (Overload resolution failed because no accessible 'New' can be called without a narrowing conversion), and I can't seem to find a lot of relevant...
2
by: Michi Henning | last post by:
Hi, the following code produces an error on the second-last line: Interface Left Sub op() End Interface Interface Right Sub op(ByVal i As Integer)
3
by: Christof Nordiek | last post by:
Given the following code Is there any way to call one of the Bar methods. using System; class Program { static void Main() { Foo<string, stringfoo = new Foo<string,string>();
1
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
I have a strange situation with vb 2005 regarding option strict that looks like a compiler bug to me. Consider: Dim f1 As New Font("Arial", 8.25) Dim f2 As New Font("Arial", 8.25,...
2
by: xtrigger303 | last post by:
Hi to all, I was reading Mr. Alexandrescu's mojo article and I've a hard time understanding the following. Let's suppose I have: //code struct A {}; struct B : A {};
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
2
by: zbigniew | last post by:
Can someone explain me how overload resolution works in C++? For example if I have function void f(char, int); and I will call f('A', 3.1) or f(1.5, 3.1F) what would be the result? Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.