#### Archived

This topic is now archived and is closed to further replies.

# overloaded function, which version will be called?

This topic is 5665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! If I made 2 member funcs of a class template, like this: // object, ''cause can''t pass // reference to basic type, ie foo(10) void foo( T obj ) { ... } // reference void foo( T &obj ) { ... } will the compiler automatically chose the reference version, when a variable is passed, and the first version, if ie. a numer is passed, or will it probably always use the first, what would result in copying the whole object every time... ? thanks It is now for sure you can throw away your computer

##### Share on other sites
*shrug*

test it and find out.

##### Share on other sites
You''d probably get an ambiguity error at compile-time.

##### Share on other sites
quote:
Original post by daerid
*shrug*

test it and find out.

duh, good idea
I just thought maybe someone *knows* this...

If it varies between different compilers,
it is not of much use to know what my compiler does.
But maybe, that's defined somewhere, and somebody
knows for sure, or even can point somewhere.

EDIT:

with above example, m$vc6 doesn't complain. but if I make such two functions that call another function overloaded in the same way, then it does. It is now for sure you can throw away your computer [edited by - UnshavenBastard on August 12, 2002 8:06:37 PM] #### Share this post ##### Link to post ##### Share on other sites It is invalid c++ to overload a function in that manner ... the fact that MSVC does not complain is irrelevant ... this violates the exact same rules which don''t let you declate 2 functions like this:  void foo(int a, int b = 0);void foo(int z);  there is no meaninfull way to tell the compiler which function is desired, and so it is invalid to create such ambiguity ... usually these sorts of errors only show up at link time, or occasionally as internal compiler errors. #### Share this post ##### Link to post ##### Share on other sites I also noticed that m$vc only compiles functions,
that are actually called somewhere in the code that
uses the template.
Whilst my old watcom 11.0c compiles the entire templates in every
header file that is included, what prevents surprises.

It is now for sure you can throw away your computer

##### Share on other sites
nothing prevents surprises ... but that was an INTENTIONAL decision of the new c++ standard ...

originally it was thought the added debugging and confidence level of complete compilation was a "good thing"

later it was shown that certain advanced template techniques could only function if unused template items were not compiled. Most of these center around the ability to define a generic template that supports a superset of behaviors depending on its intantiated type and usage scenerio. So at COMPILE TIME, if you try to use it in a way which doesn''t work with the type you feed it ... it will tell you ... notice that since the error happens at compile time, then there is no "run time" surprise ... the surprise which can happen is this though (and there is no helping it) ... the original writter of a template, cannot know for sure what will and wont work, WITHOUT specifying requirements on the instantiation type ...

sorry that was really long winded.

##### Share on other sites
quote:
// object, ''cause can''t pass
// reference to basic type,

What do you mean by that?

##### Share on other sites
quote:
Original post by UnshavenBastard
// object, ''cause can''t pass
// reference to basic type, ie foo(10)

You probably mean that a non-const reference cannot be bound to a temporary, in which case the function signature should be void "foo(const T& obj)" - it is OK for a const reference to be bound to a temporary.

##### Share on other sites
quote:
Original post by SabreMan
[quote]Original post by UnshavenBastard
// object, ''cause can''t pass
// reference to basic type, ie foo(10)

You probably mean that a non-const reference cannot be bound to a temporary, in which case the function signature should be void "foo(const T& obj)" - it is OK for a const reference to be bound to a temporary.

hey, thanks, didn''t think of const

DrPizza:

well, after reading it again, I have to
I pass pointers, but the compiler takes care of references,
so they aren''t "passed" by me, of course,
however, I guess you know what I intended to say

XAI:
well, that was what I meant with surprises.
If you write a big template in a hurry (read: hack together)
and most of its funtions get called in a program, but leaving very few of them unused, you can get the impression that this thing works,
and later, if you call a previously unused function that
has an error this is a surprise

I don''t say this is really an issue, for, you can never
expect something just hacked together to work properly,
but my old compiler did often help me
to hack something together sucessfully
Sometimes, when I just want to test _quickly_ how an idea
works, that probably gets kicked soon anyway (because I get
a better idea), I don''t want
to spend too much time to code it properly...
I suppose you know what I mean

It is now for sure you can throw away your computer