Search code examples
c++stl-algorithm

STL copy failed on array of 'typedef' type


Platform: MinGW64 (rubenvb 4.7.2), Windows 7 (64), Qt 4.8.2

Given code segment as follow:

/* type definition */
typedef long T_PSIZE;
struct A { T_PSIZE myArray[10]; };
struct B { T_PSIZE myArray[10]; };
/* declare variable */
A a;
B b;
std::copy(a.myArray[0], a.myArray[10], &b.myArray);

I don't know why the compiler throws the following error messages (similar messages also shown when changed from 'typedef long T_PSIZE;' to 'typedef int T_PSIZE;'):

> c:\mingw\rubenvb-4.7.2-64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:
> In instantiation of '_OI std::__copy_move_a(_II, _II, _OI) [with bool
> _IsMove = false; _II = long int; _OI = long int (*)[9]]': c:\mingw\rubenvb-4.7.2-64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:422:39:
> required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool
> _IsMove = false; _II = long int; _OI = long int (*)[9]]' c:\mingw\rubenvb-4.7.2-64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:454:18:
> required from '_OI std::copy(_II, _II, _OI) [with _II = long int; _OI
> = long int (*)[9]]' ..\InfluSimHKPrototype\SimApplication.cpp:114:36:   required from here
> c:\mingw\rubenvb-4.7.2-64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:375:57:
> error: no type named 'value_type' in 'struct std::iterator_traits<long
> int>'
> c:\mingw\rubenvb-4.7.2-64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:377:64:
> error: no type named 'iterator_category' in 'struct
> std::iterator_traits<long int>'
> c:\mingw\rubenvb-4.7.2-64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:381:57:
> error: no type named 'value_type' in 'struct std::iterator_traits<long
> int>'
> c:\mingw\rubenvb-4.7.2-64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:384:70:
> error: no type named 'iterator_category' in 'struct
> std::iterator_traits<long int>'

It seems that the type 'long int' cannot be recognized by the compiler's template engine. I used the similar statement with array of 'ordinary int' and it works great. I'm not using any STL containers as I know exactly the size of the target array, so I don't think I need to re-implement something like back_inserter. Do I missed something?

Note: I'm not sure if the question like this help. (Or how can I can get the 'full' qualified name for the statement to work on typedef-ed variables?)


Solution

  • You probably meant:

    std::copy(&a.myArray[0], &a.myArray[10], &b.myArray[0]);
    

    or

    std::copy(a.myArray, a.myArray + 10, b.myArray);
    

    a.myArray[0] is just a long, not a pointer into an array of long which std::copy requires. Also, the type of the output parameter needs to be compatible with the type of the object being copied. &b.myArray has type long (*)[10] whereas you need to supply a parameter of type long*.