C2825 error ?

the following code will generate a C2825 error in VS2005Beta2 July:


#include "stdafx.h"
#include <vector>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
int i = 3, j = 6;
std::swap<int>(i, j);//C2825
//but use std::swap<> <i, j>; will be ok
return 0;
}

I don't know why this , but who else can explain it to me?
many thanks.

[781 byte] By [pangwa] at [2007-12-16]
# 1
Umm, I don't see what you are trying to accomplish. std::swap is a function, you don't need that <int>, the parameters are template arguments.
RITZ at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 2

yes, I just want to pass the template parameters explicited. but it will cause a error when #include <vector> ,
the following codes works well in the Version before VS2005 July,


#include <vector>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
double i = 3, j = 6;
std::swap<double> (i, j); //C2528 when in VS 2005 July
return 0;
}

,
what cause this problem? thanks.

pangwa at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 3
Why are you doing this? You don't need to tell the function what type to use it already knows, thats the whole purpose of templates! lol.
Just do std::swap(i, j);
RITZ at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 4
generally , when using tempalte<> swap(..) it indicates thant only templates may resolve a call ,so none tempalte functions like swap(..) will not be called.
and the reason of using swap<double>.. is that I just want to know what causes this problem :-)
pangwa at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 5
Wow.. I have no idea what you are talking about. lol
RITZ at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 6
I am so sorry for my broken EnglishTongue Tied.
thanks a lot:)
pangwa at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 7
Hi: I believe that what you trying to do is to ensure that only function templates called swap will be invoked. But by explicitly providing the template arguments (int this case double) you are circumventing the type-deduction mechanism in the compiler and this is causing the compiler to choose the wrong version of swap. Luckily there is a way in C++ that you can exclude regular functions but still allow the normal type deduction to occur. You should use:

std::swap<>(i, j);

The empty "<>" lets the compiler know that it should only consider function templates but the fact that it is empty means that the regular type deduction process will occur.

Note: in this case it is unnecessary as the std namespace is reserved and the only swap functions in std are all template functions (or explicit specializations of template functions).

JonathanCavesMSFT at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...