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.



Answer this question

C2825 error ?

  • Allen Clark - MSFT

    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.


  • Dom Diaz

    I am so sorry for my broken EnglishTongue Tied.
    thanks a lot:)

  • Waseem Basheer

    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).

  • NeoNeo

    Wow.. I have no idea what you are talking about. lol

  • Eric Yeoman

    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 :-) 
       

  • Eliyahu

    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.

  • alansnoog

    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);

  • C2825 error ?