simple template question
-
Hi, I typed in the following code and found it to compile and run ok (using VS 2003).
#include < iostream > using namespace std; template void swapa(T & a, U & b) { cout << a << " " << b; } int main() { int x = 3; const double y =5.234; swapa < int , const /* should'nt the "double" be explicitly stated here? */ > (x, y); return 0; }
I am surprised of this as I did not declare the type explicitly in the invocation of swapa() where the comment is. Can someone pls explain? Many thanks! -
Hi, I typed in the following code and found it to compile and run ok (using VS 2003).
#include < iostream > using namespace std; template void swapa(T & a, U & b) { cout << a << " " << b; } int main() { int x = 3; const double y =5.234; swapa < int , const /* should'nt the "double" be explicitly stated here? */ > (x, y); return 0; }
I am surprised of this as I did not declare the type explicitly in the invocation of swapa() where the comment is. Can someone pls explain? Many thanks!minkowski wrote:
template void swapa(T & a, U & b)
Isn't it
template void <class T, class U> void swapa(T & a, U & b)
?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi, I typed in the following code and found it to compile and run ok (using VS 2003).
#include < iostream > using namespace std; template void swapa(T & a, U & b) { cout << a << " " << b; } int main() { int x = 3; const double y =5.234; swapa < int , const /* should'nt the "double" be explicitly stated here? */ > (x, y); return 0; }
I am surprised of this as I did not declare the type explicitly in the invocation of swapa() where the comment is. Can someone pls explain? Many thanks!It's a non-compliance in VS2003. Both gcc4.0.1 and VS2008 raise an error on that line: gcc4.0.1 =>
Desktop$ g++ -o a a.cpp
a.cpp: In function ‘int main()’:
a.cpp:16: error: ISO C++ forbids declaration of ‘type name’ with no typeVS2008 =>
cl -EHsc -O2 a.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.a.cpp
a.cpp(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-intJava, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p