@related: C Programming C++ struct
typedef is usefull to decribe new types :
typedef char* pchar;
typedef struct { int lenght; char* ptr; } buffer_t;
But how to rewrite this without using typedef ?
typedef int vint2[[2]];
int fAllocTypedefPtr(vint2* & a ) { //symbol is int (* &)[[2]] a = new int[[1]][[2]]; a[[0]][[0]] = 2; //ok }
int f(int (*&a)[[2]]) { } // for your curiousity
<Adrinael> boost::multi_array stores it in a contiguous array.
I suppose that typedef C keyword dont get along with arrays :
please explain me this :
typedef float vfloat3[[3]];
int main() { vfloat3 a= { 0, 1, 2 }; cout << a[[0]] << a[[1]] << a[[2]] <<endl; // ok : 012 vfloat3 b; // b=a; // error: ISO C++ forbids assignment of arrays //gcc complain return 0; }
Of course if you replace vfloat3 by float3 , it does not make sense but why allowing typedef with arrays if you can not fully use them ?
Doesnt work better with C++ STL
#include <vector> #include <iostream> using namespace std;
typedef float vfloat3[[3]];
int main() {
vfloat3 a= { 0, 1, 2 }; cout << a[[0]] << a[[1]] << a[[2]] <<endl; // ok : 012 vector<vfloat3> v; cout<<v.size() <<endl; // ok : 0 v.push_back(a); //gives errors on compilation (g++ and msvc) return 0; }
/*
/usr/include/c++/3.3.1/bits/stl_vector.h:603: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [[with _Tp = float[[3]], _Alloc = std::allocator<float[[3]]>]]'
c:\program files\microsoft visual studio\vc98\include\xutility(39) : error C2440: '=' : cannot convert from 'const float [[3]]' to 'float [[3]]' There is no context in which this conversion is possible c:\program files\microsoft visual studio\vc98\include\vector(170) : see reference to function template instantiation 'void __cdecl std::fill(float (*)[[3]],float (*)[[3]],const float (&)[[3]])' being compiled c:\program files\microsoft visual studio\vc98\include\xutility(25) : error C2106: '=' : left operand must be l-value c:\program files\microsoft visual studio\vc98\include\vector(174) : see reference to function template instantiation 'float (*__cdecl std::copy_backward(float (*)[[3]],float (*)[[3]],float (*)[[3]]))[[3]]' being compiled c:\program files\microsoft visual studio\vc98\include\xmemory(34) : error C2538: new : cannot specify initializer for arrays c:\program files\microsoft visual studio\vc98\include\xmemory(66) : see reference to function template instantiation 'void __cdecl std::_Construct(float (*)[[3]],const float (&)[[3]])' being compiled
<MooZ> RzR: push_back needs the = operator to be defined
<MooZ> so that's why u have an error <MooZ> have u tried: <MooZ> typedef vfloat3{ <MooZ> union { <MooZ> float data[[3]]; <MooZ> struct{ float x,y,z; }; <MooZ> };
<Doudou> (a propos de la page) le premier probleme, ca marche pas parce que un tableau C et C++ sur un type T est un "T const*" donc b = a ca merde <Doudou> Le deuxieme, ca merde pour exactement pour la meme raison. <Doudou> std::vector<T> a besoin que T soit "assignable", autrement dit que l'opérateur = soit valide, ce qui n'est pas le cas ici <Doudou> std::list, par contre, a juste besoin d'un constructeur par copie. Donc ca marche ..