Skip to main content
GameDev.net gamedev.net
🔒 Locked

templates/function overloading

Started by Endar Jun 5, 2006 at 1:54 AM 7 replies 550+ views
Original Post
Endar
Endar

template <class T0, class T1>
void function(T0 i, T1 c)
{
	std::cout << __FUNCTION__ << ": (" << i << ", " << c << ")" << std::endl;
}

int main()
{
	function( "hello there", 345 );

return 0;
}

Since when did C++ do this? I am talking about the compiling correct template function without having to specify the template arguments (not the FUNCTION macro). Is this in the standard? Is it only recent compilers that do this? Or have I just completely missed seeing this for the last 2 years?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Endar
Endar
Quote:
Original post by omgomghilol
You've missed it.

Damn.

Anything else that might be not easily noticed but useful and important?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
moussen15
moussen15
Well I beleive You don't have to have that "return 0;" at the end of your main-function anymore (if it ever was necessary) ;) But that might not be very useful and/or important :(
joanusdmentia
joanusdmentia
Quote:
Original post by Endar
Anything else that might be not easily noticed but useful and important?


class Test{public:    template<typename T> Test& operator=(const T& val)    {        std::cout << "operator=() called" << std::endl;    }};int main(){    Test a;    Test b;    a = b;}


.....won't call the operator=() that you defined.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
Endar
Endar
Quote:
Original post by joanusdmentia
Quote:
Original post by Endar
Anything else that might be not easily noticed but useful and important?


*** Source Snippet Removed ***

.....won't call the operator=() that you defined.


Really? Why? Will it just call the default assignment operator for the two types?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
omgomghilol
omgomghilol
Because if you have something like the following:

void f(int i){}template <typename t> void f(T t){}int main(){    int i = 3;    f(3);}


It will call the normal version of the function; to use the template you'd have to do f(i);

In the case of the = operator, the compiler provides one by default that will be used instead of your template version.
Sharlin
Sharlin
The same rule applies to copy constructors - the following won't override the compiler-generated foo::foo(foo const&):
class foo {  template <typename T>  foo(T const& t) { /* stuff */ }};
Zahlman
Zahlman
It's called "template inference", and free functions do it - class functions generally cannot.

It is used in the standard library (see std::make_pair()), so yes, you have been missing out on it for quite a while.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.