PRB: C2664 Instantiating STL List with Sequence [first, last)

Last reviewed: July 24, 1997
Article ID: Q168404
The information in this article applies to:
  • Microsoft Visual C++, 32-bit Editions, version 5.0

SYMPTOMS

Instantiating an STL list container with a sequence [first, last), where first and last are iterators, causes the compiler error C2664:

   'function': cannot convert parameter number from 'type1' to 'type2'

CAUSE

Using the list(const_iterator first, const_iterator last, const A& a1= A()) constructor causes the compiler error C2664. The constructor for list takes a list<T>::const_iterator instead of a generic iterator for the first two parameters.

RESOLUTION

Workaround 1

Use the default constructor. Insert elements in the list using the push_back member function.

Workaround 2

Use the list(size_type, const T& v = T(), const A& a1 = A()) constructor. Insert elements in the list using the copy algorithm.

STATUS

Microsoft is researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

The following sample demonstrates the problem:

   //test1.cpp
   //Compiler options needed: /GX

   #include <list>

   int main()
   {
      int INTARRAY[4] = {1, 2, 3, 4} ;
      std::list<int> INTLIST(INTARRAY, INTARRAY + 4) ; //C2664 here
   }

The exact error message for the above sample is:

   test1.cpp(9) : error C2664: 'std::list<int,class
   std::allocator<int>>::list<int,class std::allocator<int>>(unsigned
   int,const int &,const class std::allocator<int> &)' : cannot convert
   parameter 1 from 'int [4]' to 'unsigned int'

The following sample demonstrates Workaround 1:

   //test2.cpp
   //Compiler options needed: /GX

   #include <list>

   int main()
   {
      int INTARRAY[4] = {1, 2, 3, 4} ;
      std::list<int> INTLIST ;
      for(int i = 0; i < 4; i++)
          INTLIST.push_back(INTARRAY[i]) ;
      return 0 ;
   }

The following sample demonstrates Workaround 2:

   //test3.cpp
   //Compiler options needed: /GX

   #include <list>
   #include <algorithm>

   int main()
   {
      int INTARRAY[4] = {1, 2, 3, 4} ;
      std::list<int> INTLIST(4) ;
      std::copy(INTARRAY, INTARRAY + 4, INTLIST.begin()) ;
      return 0 ;
   }


Additional query words: list copy push_back
Keywords : CPPIss STLIss vcbuglist500
Version : 5.0
Platform : NT WINDOWS
Issue type : kbprb


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 24, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.