473,378 Members | 1,416 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

is this class POD ?

template <class T, unsigned size>
struct Array {
typedef unsigned SizeType;
typedef T ValueType;
typedef T* Ptr;
typedef T const* ConstPtr;

// Default copy ctor and assignment op are fine.

SizeType Size() const { return size; }

T& operator[](SizeType i) { return m_array[i]; }
T const& operator[](SizeType i) const { return m_array[i]; }

Ptr Begin() { return &m_array[0]; }
ConstPtr Begin() const { return &m_array[0]; }

Ptr End() { return &m_array[0] + size; }
ConstPtr End() const { return &m_array[0] + size; }

private:
ValueType m_array[size];
};

It defines neither a copy-ctor nor a destructor.
Does the presence of private section render it as non-POD?

I assume that, if the above class was a POD type, objects of the
following types T and U would end up with equivalent memory layout:

typedef float TX[3];
typedef TX TY[4];

typedef Array<float, 3> UX;
typedef Array<UX, 4> UY;

typedef TY T;
typedef UY U;

Thanks.
--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #1
12 1733
bartek wrote in news:Xn**********************************@153.19.2 51.200 in
comp.lang.c++:

Subject: is this class POD ?
template <class T, unsigned size>
struct Array {
[snip POD-ish stuff]

private:
ValueType m_array[size];
};

It defines neither a copy-ctor nor a destructor.
Does the presence of private section render it as non-POD?

The private section isn't the problem, its the private *data*.
I assume that, if the above class was a POD type, objects of the
following types T and U would end up with equivalent memory layout:

typedef float TX[3];
typedef TX TY[4];

typedef Array<float, 3> UX;
typedef Array<UX, 4> UY;

typedef TY T;
typedef UY U;


I don't really understand the above, but it doesn't matter
as Array<> isn't a POD.

Unfortunatly POD only really exists as a concept to provide
compatibility with how C ( no ++ ) does things, useful uses
beyond that are rare.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
Rob Williscroft <rt*@freenet.co.uk> wrote in
news:Xn**********************************@130.133. 1.4:
bartek wrote in
news:Xn**********************************@153.19.2 51.200 in
comp.lang.c++:

Subject: is this class POD ?
template <class T, unsigned size>
struct Array {


[snip POD-ish stuff]

private:
ValueType m_array[size];
};

It defines neither a copy-ctor nor a destructor.
Does the presence of private section render it as non-POD?


The private section isn't the problem, its the private *data*.


That's what I actually meant.
In that case, making the data public would result in the Array class
becoming POD-compliant?
I assume that, if the above class was a POD type, objects of the
following types T and U would end up with equivalent memory layout:

typedef float TX[3];
typedef TX TY[4];

typedef Array<float, 3> UX;
typedef Array<UX, 4> UY;

typedef TY T;
typedef UY U;


I don't really understand the above, but it doesn't matter
as Array<> isn't a POD.

Unfortunatly POD only really exists as a concept to provide
compatibility with how C ( no ++ ) does things, useful uses
beyond that are rare.


Sorry for putting it in a somewhat contrived way.
Indeed C compatibility is what I'm going after.

Say, I have a C library which uses similar typedefs around in its
interfaces:

typedef float my_vector_t[3];
typedef float my_matrix_t[4][4];

I need to use those interfaces from C++ code.
Though, I'd like to keep out from using C arrays in my interfaces, so
wrapping arrays into a class seems a reasonable solution. Moreover, if
those C++ wrapped arrays were binary-compatible with C arrays, I could
gain both performance and unification.

If I was sure that the following C++ objects and C arrays had equivalent
binary representations, I could get rid of explicit conversions on
interface borders, and get the adventage of using strict types throughout
my program.

float x[4][4];
Array<Array<float, 4>, 4> y;

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #3
bartek wrote in news:Xn**********************************@153.19.2 51.200
in comp.lang.c++:
The private section isn't the problem, its the private *data*.
That's what I actually meant.
In that case, making the data public would result in the Array class
becoming POD-compliant?


Yes.


[snip]

Unfortunatly POD only really exists as a concept to provide
compatibility with how C ( no ++ ) does things, useful uses
beyond that are rare.


Sorry for putting it in a somewhat contrived way.
Indeed C compatibility is what I'm going after.

Say, I have a C library which uses similar typedefs around in its
interfaces:

typedef float my_vector_t[3];
typedef float my_matrix_t[4][4];

I need to use those interfaces from C++ code.
Though, I'd like to keep out from using C arrays in my interfaces, so
wrapping arrays into a class seems a reasonable solution. Moreover, if
those C++ wrapped arrays were binary-compatible with C arrays, I could
gain both performance and unification.

If I was sure that the following C++ objects and C arrays had
equivalent binary representations, I could get rid of explicit
conversions on interface borders, and get the adventage of using
strict types throughout my program.

float x[4][4];
Array<Array<float, 4>, 4> y;


eg

struct xplusplus
{
float x[4][4];
// whatever ...
};

IIU( The Standard )C, x is at offset 0 of xplusplus, but you may
be able to find a platform where sizeof( xplusplus ) > sizeof( x ).

So maybe:

Array<Array<float, 4>, 4> &y = *
reinterpret_cast< Array<Array<float, 4>, 4> * >( x )
;

Will work Ok until you try copying your Array.

Don't do it its UB. If you get your code working, your tying
your code to your current platform and your current compiler.

If you show some samples of the C API your dealing with then
myself or others may be able to give some better advice on
how best to deal with it.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
"bartek" <sp******************@o2.pl> wrote in message
news:Xn**********************************@153.19.2 51.200
template <class T, unsigned size>
struct Array {
typedef unsigned SizeType;
typedef T ValueType;
typedef T* Ptr;
typedef T const* ConstPtr;

// Default copy ctor and assignment op are fine.

SizeType Size() const { return size; }

T& operator[](SizeType i) { return m_array[i]; }
T const& operator[](SizeType i) const { return m_array[i]; }

Ptr Begin() { return &m_array[0]; }
ConstPtr Begin() const { return &m_array[0]; }

Ptr End() { return &m_array[0] + size; }
ConstPtr End() const { return &m_array[0] + size; }

private:
ValueType m_array[size];
};

It defines neither a copy-ctor nor a destructor.
Does the presence of private section render it as non-POD?

I assume that, if the above class was a POD type, objects of the
following types T and U would end up with equivalent memory layout:

typedef float TX[3];
typedef TX TY[4];

typedef Array<float, 3> UX;
typedef Array<UX, 4> UY;

typedef TY T;
typedef UY U;

Thanks.
--
bartekd [at] o2 [dot] pl

http://www.parashift.com/c++-faq-lit....html#faq-26.7
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 22 '05 #5
Rob Williscroft <rt*@freenet.co.uk> wrote in
news:Xn**********************************@130.133. 1.4:

(...)
eg

struct xplusplus
{
float x[4][4];
// whatever ...
};

IIU( The Standard )C, x is at offset 0 of xplusplus, but you may
be able to find a platform where sizeof( xplusplus ) > sizeof( x ).


Of course, it seems I was missing the obvious all the time.

Somehow, I assumed that:

struct X {
float x;
};

being a POD, should be equivalent to:

float x;

Obviously, I was missing the point. POD-ness is not relevant here.
Thanks for pointing that out.

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #6
"bartek" <sp******************@o2.pl> wrote
template <class T, unsigned size>
struct Array {
typedef unsigned SizeType;
typedef T ValueType;
typedef T* Ptr;
typedef T const* ConstPtr;

// Default copy ctor and assignment op are fine.

SizeType Size() const { return size; }

T& operator[](SizeType i) { return m_array[i]; }
T const& operator[](SizeType i) const { return m_array[i]; }

Ptr Begin() { return &m_array[0]; }
ConstPtr Begin() const { return &m_array[0]; }

Ptr End() { return &m_array[0] + size; }
ConstPtr End() const { return &m_array[0] + size; }

private:
ValueType m_array[size];
};
Because this is a template, whether or not an instantiation is a POD type depends
on the template arguments. An Array<double, 5> would be a POD type, but an
Array<std::string, 7> would not because std::string itself is not a POD. The
presence of typedefs and non-virtual member functions doesn't make a class a
non-POD type.
It defines neither a copy-ctor nor a destructor.
That's irrelevant to determining whether a class is a POD type.
Does the presence of private section render it as non-POD?


No. POD types are allowed to have private sections.

It's a loose convention to make POD types structs (of PODs) with only data
members, but it's not a technical limitation. A class with only POD data members
and with only non-virtual member functions is itself a POD, regardless of how you
set up accessibility. To be even more evil, a class could have non-POD _static_
data members and still be a POD type. :-)

Claudio Puviani

Jul 22 '05 #7
"Claudio Puviani" <pu*****@hotmail.com> wrote in
news:wk***********************@news4.srv.hcvlny.cv .net:
"bartek" <sp******************@o2.pl> wrote
template <class T, unsigned size>
struct Array {
typedef unsigned SizeType;
typedef T ValueType;
typedef T* Ptr;
typedef T const* ConstPtr;

// Default copy ctor and assignment op are fine.

SizeType Size() const { return size; }

T& operator[](SizeType i) { return m_array[i]; }
T const& operator[](SizeType i) const { return m_array[i]; }

Ptr Begin() { return &m_array[0]; }
ConstPtr Begin() const { return &m_array[0]; }

Ptr End() { return &m_array[0] + size; }
ConstPtr End() const { return &m_array[0] + size; }

private:
ValueType m_array[size];
};


Because this is a template, whether or not an instantiation is a POD
type depends on the template arguments. An Array<double, 5> would be a
POD type, but an Array<std::string, 7> would not because std::string
itself is not a POD. The presence of typedefs and non-virtual member
functions doesn't make a class a non-POD type.


Yes of course you're right. I somehow didn't mention the 'background'
information regarding the class. Indeed, regarding POD-ness, I meant it
only for primitive types.
It defines neither a copy-ctor nor a destructor.


That's irrelevant to determining whether a class is a POD type.
Does the presence of private section render it as non-POD?


No. POD types are allowed to have private sections.

It's a loose convention to make POD types structs (of PODs) with only
data members, but it's not a technical limitation. A class with only
POD data members and with only non-virtual member functions is itself
a POD, regardless of how you set up accessibility. To be even more
evil, a class could have non-POD _static_ data members and still be a
POD type. :-)


Soon after posting the question, I've realised that I misinterpreted the
definition of POD.

While POD reserves binary compatibility with C structs, I have confused
it with some sort of "encapsulated-type equivalence"... That is, I
thought that a class (no bases, no virtuals, no non-trivial copy or
destruction) with a single data member of type T, is guaranteed to have
the same memory layout as the type T by itself.

I've assumed that:

struct X {
float f[3];
};

has the same binary representation as:

typedef float Y[3];

so that:

X array_of_x[10];

ends up with the same memory layout as:

Y array_of_y[10];

Now I realise that, though, it seems to work on many platforms,
of course, it is not guaranteed.

Cheers.
--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #8
Claudio Puviani wrote in news:wk0nc.164606$Gd3.45000232
@news4.srv.hcvlny.cv.net in comp.lang.c++:
It defines neither a copy-ctor nor a destructor.


That's irrelevant to determining whether a class is a POD type.


From 9/4:

A POD-struct is an aggregate class that has no non-static data
members of type non-POD-struct, non-POD-union (or array of such
types) or reference, and has no user-defined copy assignment
operator and no user-defined destructor.

"aggregate class" (8.5.1) may not have user defined ctor's, so any
ctor/dtor is right out.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #9
"Rob Williscroft" <rt*@freenet.co.uk> wrote
Claudio Puviani wrote
It defines neither a copy-ctor nor a destructor.


That's irrelevant to determining whether a class is a POD type.


From 9/4:

A POD-struct is an aggregate class that has no non-static data
members of type non-POD-struct, non-POD-union (or array of such
types) or reference, and has no user-defined copy assignment
operator and no user-defined destructor.

"aggregate class" (8.5.1) may not have user defined ctor's, so any
ctor/dtor is right out.


Thank you. If I'd taken a moment to think about it, it should have been obvious
to me even without checking the standard, since a memcopy or memset would violate
any contraints imposed by those two functions.

Claudio Puviani
Jul 22 '05 #10
"Claudio Puviani" <pu*****@hotmail.com> wrote in message
news:wk***********************@news4.srv.hcvlny.cv .net
A class with only
POD data members and with only non-virtual member functions is itself
a POD, regardless of how you set up accessibility. To be even more
evil, a class could have non-POD _static_ data members and still be a
POD type. :-)

Further to Rob's point, 8.5.1 p1 reads:

An aggregate is an array or a class (clause 9) with no user-declared
constructors (12.1), no private or protected non-static data members (clause
11), no base classes (clause 10), and no virtual functions (10.3).
Thus non-static private (and protected) members are ruled out.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #11
"John Carson" <do***********@datafast.net.au> wrote
"Claudio Puviani" <pu*****@hotmail.com> wrote
A class with only
POD data members and with only non-virtual member functions is itself
a POD, regardless of how you set up accessibility. To be even more
evil, a class could have non-POD _static_ data members and still be a
POD type. :-)

Further to Rob's point, 8.5.1 p1 reads:

An aggregate is an array or a class (clause 9) with no user-declared
constructors (12.1), no private or protected non-static data members (clause
11), no base classes (clause 10), and no virtual functions (10.3).
Thus non-static private (and protected) members are ruled out.


Again one of those things that 30 more seconds of thought would have brought up.
Since compilers are allowed to reorder data members that have different
accessibility, obviously (in retrospect), classes with mixed access specifiers
can't be POD types.

Interestingly, since a class that has all data members either private or
protected shares the same physical attributes as one that has only public data
members, we can infer that an implicit requirement for a POD is that it must
allow static initializers like:

XYZ xyz = { 1, 'b', 2.7 };

I'm not sure that this limitation is particularly meanigful or useful, but then
again, it doesn't even rank as high as a minor irritant.

Thanks for pointing out clause 11. I need to stop posting at 4am. ;-)

Claudio Puviani
Jul 22 '05 #12
"Claudio Puviani" <pu*****@hotmail.com> wrote in message news:<ux***********************@news4.srv.hcvlny.c v.net>...

[ ... ]
Since compilers are allowed to reorder data members that have different
accessibility, obviously (in retrospect), classes with mixed access specifiers
can't be POD types.


Technically, they can be, but they don't provide the guarantees that
people normally associate with POD types. The wording of the standard
also has one other little oddity in this area: even a vacuous access
specifier has the same effect. Consider, the following:

struct X {
int x;
public: int y;
public: int z;
};

This still fits the standard's definition of POD but the relative
order of y and z is not guaranteed, even though neither "public:" has
changed accessibility of anything.

This problem should probably be forwarded to the standards committee,
but I've had a few too many bouts with censorship on
comp.lang.c++.moderated and comp.std.c++, so the dubious honor of
doing so will have to fall to somebody else.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.