Compilation problem when upgrading from g++ 3.3 to g++ 3.4

A

Alex Vinokur

Hi,

The code below has no problem with GNU g++ 3.3,
but it has a problem with GNU g++ 3.4.

Any suggestions?

------ foo.cpp ------
#include <vector>
using namespace std;

typedef char foo_t[3];
#define ELEM1 "ab"
#define ELEM2 "cd"

const foo_t a[] = {ELEM1, ELEM2};
const vector<foo_t> v = vector<foo_t>(a, a + sizeof (a)/sizeof (*a));

int main()
{
return 0;
}
---------------------



------ Compilation with GNU gcc 3.3 (Cygwin) : BEGIN ------

$ g++ --version
g++ (GCC) 3.3.3 (cygwin special)
[---omitted---]

$ g++ -W -Wall foo.cpp
// No errors/warnings

------ Compilation with GNU gcc 3.3 (Cygwin) : END --------



------ Compilation with GNU gcc 3.4 (DJGPP) : BEGIN ------

$ gpp --version
gpp.exe (GCC) 3.4.1
[---omitted---]


$ gpp -W -Wall foo.cpp
c:/djgpp/bin/../lib/gcc/djgpp/3.41/../../../../include/cxx/3.41/bits/stl_construct.h: In function `void std::_Construct(_T1*, const
_T2&) [with _T1 = char[3], _T2 = char[3]]':
c:/djgpp/bin/../lib/gcc/djgpp/3.41/../../../../include/cxx/3.41/bits/stl_uninitialized.h:86: instantiated from `_ForwardIterator
std::__uninitialized_copy_aux(_InputIterator, _InputIterator, _ForwardIterator, __false_type) [with _InputIterator =
__gnu_cxx::__normal_iterator<const char (*)[3], std::vector<char[3], std::allocator<char[3]> > >, _ForwardIterator = char (*)[3]]'
c:/djgpp/bin/../lib/gcc/djgpp/3.41/../../../../include/cxx/3.41/bits/stl_uninitialized.h:112: instantiated from `_ForwardIterator
std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const
char (*)[3], std::vector<char[3], std::allocator<char[3]> > >, _ForwardIterator = char (*)[3]]'
c:/djgpp/bin/../lib/gcc/djgpp/3.41/../../../../include/cxx/3.41/bits/stl_vector.h:221: instantiated from `std::vector<_Tp,
_Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = char[3], _Alloc = std::allocator<char[3]>]'
foo.cpp:9: instantiated from here
c:/djgpp/bin/../lib/gcc/djgpp/3.41/../../../../include/cxx/3.41/bits/stl_construct.h:81: error: ISO C++ forbids initialization in
array new

------ Compilation with GNU gcc 3.4 (DJGPP) : END --------
 
J

John Harrison

Alex Vinokur said:
Hi,

The code below has no problem with GNU g++ 3.3,
but it has a problem with GNU g++ 3.4.

Any suggestions?

Arrays cannot be members of STL containers because they are not copyable or
assignable. No idea why g++ 3.3 allowed it in the first place.
------ foo.cpp ------
#include <vector>
using namespace std;

typedef char foo_t[3];
#define ELEM1 "ab"
#define ELEM2 "cd"

const foo_t a[] = {ELEM1, ELEM2};
const vector<foo_t> v = vector<foo_t>(a, a + sizeof (a)/sizeof (*a));

int main()
{
return 0;
}

Try something like this

class Char3
{
public:
Char3(const char*);
char operator[](size_t i);
char c[3];
};

const Char3 a[] = {ELEM1, ELEM2};
const vector<Char3> v = vector<Char3>(a, a + sizeof (a)/sizeof (*a));

john
 
R

Ron Natalie

John said:
Arrays cannot be members of STL containers because they are not copyable or
assignable. No idea why g++ 3.3 allowed it in the first place.
G++ has an extension to give copy semantics to arrays. The later versions
of G++ (I assumed that this happened before 3.3 though) turn the extensions
off by default.
 
M

Michiel Salters

John Harrison said:
Arrays cannot be members of STL containers because they are not copyable or
assignable. No idea why g++ 3.3 allowed it in the first place.

It might have used memcpy, which would be a legal implementation
since char[3] is a POD. You just can't rely on such an implementation,
as g++3.4 shows.

Regards,
Michiel Salters
 
T

Tommi =?ISO-8859-1?Q?M=E4kitalo?=

Hi,

g++ 3.4 is more pinky about right c++. A std::vector initializes elements
with new. You define a std::vector of a array, so it should use new[]. It
is not allowed to define a std::vector of array.

Tommi


Alex said:
Hi,

The code below has no problem with GNU g++ 3.3,
but it has a problem with GNU g++ 3.4.

Any suggestions?

------ foo.cpp ------
#include <vector>
using namespace std;

typedef char foo_t[3];
#define ELEM1 "ab"
#define ELEM2 "cd"

const foo_t a[] = {ELEM1, ELEM2};
const vector<foo_t> v = vector<foo_t>(a, a + sizeof (a)/sizeof (*a));

int main()
{
return 0;
}
---------------------



------ Compilation with GNU gcc 3.3 (Cygwin) : BEGIN ------

$ g++ --version
g++ (GCC) 3.3.3 (cygwin special)
[---omitted---]

$ g++ -W -Wall foo.cpp
// No errors/warnings

------ Compilation with GNU gcc 3.3 (Cygwin) : END --------



------ Compilation with GNU gcc 3.4 (DJGPP) : BEGIN ------

$ gpp --version
gpp.exe (GCC) 3.4.1
[---omitted---]


$ gpp -W -Wall foo.cpp
c:/djgpp/bin/../lib/gcc/djgpp/3.41/../../../../include/cxx/3.41/bits/stl_construct.h:
In function `void std::_Construct(_T1*, const _T2&) [with _T1 = char[3],
_T2 = char[3]]':
c:/djgpp/bin/../lib/gcc/djgpp/3.41/../../../../include/cxx/3.41/bits/stl_uninitialized.h:86:
instantiated from `_ForwardIterator
std::__uninitialized_copy_aux(_InputIterator, _InputIterator,
_ForwardIterator, __false_type) [with _InputIterator =
__gnu_cxx::__normal_iterator<const char (*)[3], std::vector<char[3],
std::allocator<char[3]> > >, _ForwardIterator = char (*)[3]]'
c:/djgpp/bin/../lib/gcc/djgpp/3.41/../../../../include/cxx/3.41/bits/stl_uninitialized.h:112:
instantiated from `_ForwardIterator
std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator)
[with _InputIterator = __gnu_cxx::__normal_iterator<const char (*)[3],
std::vector<char[3], std::allocator<char[3]> > >, _ForwardIterator = char
(*)[3]]'
c:/djgpp/bin/../lib/gcc/djgpp/3.41/../../../../include/cxx/3.41/bits/stl_vector.h:221:
instantiated from `std::vector<_Tp, _Alloc>::vector(const
std::vector<_Tp, _Alloc>&) [with _Tp = char[3], _Alloc =
std::allocator<char[3]>]'
foo.cpp:9: instantiated from here
c:/djgpp/bin/../lib/gcc/djgpp/3.41/../../../../include/cxx/3.41/bits/stl_construct.h:81:
error: ISO C++ forbids initialization in array new

------ Compilation with GNU gcc 3.4 (DJGPP) : END --------

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top