overloaded function, which version will be called?

Started by
8 comments, last by UnshavenBastard 21 years, 7 months ago
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
Advertisement
*shrug*

test it and find out.
daerid@gmail.com
You''d probably get an ambiguity error at compile-time.
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:

about ambiguity:
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]
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.

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
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.
quote:// object, ''cause can''t pass
// reference to basic type,

What do you mean by that?

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
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.
quote:Original post by SabreMan
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
admit this comment is nonsense
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

This topic is closed to new replies.

Advertisement