G
Guest
Hi All,
I'm not sure if I'm dealing with a C++ question or a compiler question,
so please forgive me if I'm asking in the wrong spot. If so, maybe
someone can direct me to more appropriate spot.
In migrating from gcc 3.2.3 to gcc 3.4.3 the following situation comes
up.
I have the following:
//typedef std::vector<TestClass* > TestClassVector;
typedef std::vector<TestClass*,TestAllocator<TestClass> >
TestClassVector;
TestClassVector a;
TestClassVector * tmpVecPtr = &a;
TestClass * tmpPtr = new TestClass(5);
a.push_back(tmpPtr);
if ( (*tmpVecPtr)[0] != NULL )
cout<<"hello main"<<endl;
When using the custom allocator this line breaks:
(*tmpVecPtr)[0] != NULL )
thinking that this field is a TestClass and opposed to the TestClass*
which it should be. If I use the first typedef(with no allocator
specified) it compiles fine with gcc 3.4.3. Also both compile fine
under gcc 3.2.3
I'm not clear if there has been a tightening compliance of the compiler
and i need to add a method to the allocator class to make this compile,
or what exactly my problem is.
If anyone can shed some light on this I would be very appreciative.
Thanks much!
I'll attach the complete code below:
#include <iostream>
#include <vector>
using namespace std;
#ifndef MEMORY_H
#include <memory>
#define MEMORY_H
#endif
template <class T>
class TestAllocator;
template <>
class TestAllocator<void>
{
public:
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
template <class U>
struct rebind
{
typedef TestAllocator<U> other;
};
};
template <class T>
class TestAllocator
{
public:
// Type definitions
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
pointer address(reference value) const { return &value; }
const_pointer address(const_reference value) const { return &value;
}
// Constructors and destructor
TestAllocator() throw() {}
TestAllocator(const TestAllocator& copy) {}
template <class U>
TestAllocator(const TestAllocator<U>&) throw() {}
~TestAllocator() throw() {}
// Assignment Operator
TestAllocator& operator=(const TestAllocator& rhs) { return *this;
}
inline pointer
allocate(size_type n, const_pointer = 0)
{
pointer p;
size_type size;
size = n * sizeof(T);
p = (pointer) :perator new(size);
return p;
}
inline void
deallocate(pointer p, size_type n = 1)
{
:perator delete(p);
}
void
construct(pointer p, const T& val)
{
new((void*) p) T(val);
}
void
destroy(pointer p)
{
((T*) p)->~T();
}
size_type
max_size() const throw()
{
return std::numeric_limits<std::size_t>::max() / sizeof(T);
}
template <class U>
struct rebind
{
typedef TestAllocator<U> other;
};
};
class TestClass
{
public:
TestClass(int a): b(a){};
~TestClass();
int getNum() { return b;};
int b;
};
//typedef std::vector<TestClass* > TestClassVector;
typedef std::vector<TestClass*,TestAllocator<TestClass> >
TestClassVector;
int main(int argc, char *argv[])
{
TestClassVector a;
TestClassVector * tmpVecPtr = &a;
TestClass * tmpPtr = new TestClass(5);
a.push_back(tmpPtr);
if ( (*tmpVecPtr)[0] != NULL )
cout<<"hello main"<<endl;
}
I'm not sure if I'm dealing with a C++ question or a compiler question,
so please forgive me if I'm asking in the wrong spot. If so, maybe
someone can direct me to more appropriate spot.
In migrating from gcc 3.2.3 to gcc 3.4.3 the following situation comes
up.
I have the following:
//typedef std::vector<TestClass* > TestClassVector;
typedef std::vector<TestClass*,TestAllocator<TestClass> >
TestClassVector;
TestClassVector a;
TestClassVector * tmpVecPtr = &a;
TestClass * tmpPtr = new TestClass(5);
a.push_back(tmpPtr);
if ( (*tmpVecPtr)[0] != NULL )
cout<<"hello main"<<endl;
When using the custom allocator this line breaks:
(*tmpVecPtr)[0] != NULL )
thinking that this field is a TestClass and opposed to the TestClass*
which it should be. If I use the first typedef(with no allocator
specified) it compiles fine with gcc 3.4.3. Also both compile fine
under gcc 3.2.3
I'm not clear if there has been a tightening compliance of the compiler
and i need to add a method to the allocator class to make this compile,
or what exactly my problem is.
If anyone can shed some light on this I would be very appreciative.
Thanks much!
I'll attach the complete code below:
#include <iostream>
#include <vector>
using namespace std;
#ifndef MEMORY_H
#include <memory>
#define MEMORY_H
#endif
template <class T>
class TestAllocator;
template <>
class TestAllocator<void>
{
public:
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
template <class U>
struct rebind
{
typedef TestAllocator<U> other;
};
};
template <class T>
class TestAllocator
{
public:
// Type definitions
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
pointer address(reference value) const { return &value; }
const_pointer address(const_reference value) const { return &value;
}
// Constructors and destructor
TestAllocator() throw() {}
TestAllocator(const TestAllocator& copy) {}
template <class U>
TestAllocator(const TestAllocator<U>&) throw() {}
~TestAllocator() throw() {}
// Assignment Operator
TestAllocator& operator=(const TestAllocator& rhs) { return *this;
}
inline pointer
allocate(size_type n, const_pointer = 0)
{
pointer p;
size_type size;
size = n * sizeof(T);
p = (pointer) :perator new(size);
return p;
}
inline void
deallocate(pointer p, size_type n = 1)
{
:perator delete(p);
}
void
construct(pointer p, const T& val)
{
new((void*) p) T(val);
}
void
destroy(pointer p)
{
((T*) p)->~T();
}
size_type
max_size() const throw()
{
return std::numeric_limits<std::size_t>::max() / sizeof(T);
}
template <class U>
struct rebind
{
typedef TestAllocator<U> other;
};
};
class TestClass
{
public:
TestClass(int a): b(a){};
~TestClass();
int getNum() { return b;};
int b;
};
//typedef std::vector<TestClass* > TestClassVector;
typedef std::vector<TestClass*,TestAllocator<TestClass> >
TestClassVector;
int main(int argc, char *argv[])
{
TestClassVector a;
TestClassVector * tmpVecPtr = &a;
TestClass * tmpPtr = new TestClass(5);
a.push_back(tmpPtr);
if ( (*tmpVecPtr)[0] != NULL )
cout<<"hello main"<<endl;
}