Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problems with void Template Type Parameters and Zero-Length Arrays

Reply
Thread Tools

Problems with void Template Type Parameters and Zero-Length Arrays

 
 
Matt Taylor
Guest
Posts: n/a
 
      09-22-2004
I am trying to use templates to create an optimal structure for a fixed-size
buffer that holds a homogenous array of elements. For brevity, this problem
is somewhat simplified from my implementation:

template<typename T1, size_t len>
struct buffer_t
{
// Array of elements
T1 ary[len / sizeof(T1)];

// Align upward to a multiple of sizeof(T1)
char pad[len - ((len / sizeof(T1)) * sizeof(T1))];
};

The problem is that, when len is a multiple of sizeof(T1), the pad variable
has length 0. For some reason the compiler finds this to be absurd. I need
to find some way to make this compile in that case. Is there any simple
resolution to this?

I am also wondering about the following:
template<typename T>
struct A
{
int a;
T b;
};

struct INGET
{
};

Why is A<INGET> legal but not A<void>? Semantically they would be the same,
right?

-Matt


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      09-22-2004
Matt Taylor wrote in news:nQn4d.45958$(E-Mail Removed)
in comp.lang.c++:

>
> The problem is that, when len is a multiple of sizeof(T1), the pad
> variable has length 0. For some reason the compiler finds this to be
> absurd. I need to find some way to make this compile in that case. Is
> there any simple resolution to this?
>


#include <iostream>
#include <cstddef>

using std::size_t;

template <typename T1, size_t len>
struct padding
{
static size_t const value = len - ((len / sizeof(T1)) * sizeof(T1));
};

template < typename T, size_t Count, size_t Pad >
struct base_buffer_t
{
T ary[ Count ];
char pad[ Pad ];
};

template < typename T, size_t Count >
struct base_buffer_t< T, Count, 0U >
{
T ary[ Count ];
typedef void pad; /* for using */
};


template <typename T1, size_t len>
struct buffer_t :
private base_buffer_t<
T1, len / sizeof(T1), len - ((len / sizeof(T1)) * sizeof(T1))
>

{
private:

typedef base_buffer_t<
T1, len / sizeof(T1), len - ((len / sizeof(T1)) * sizeof(T1))
>

base_t
;

public:

using base_t::ary;
using base_t:ad;
};


int main()
{
buffer_t< int, sizeof( int ) * 2 > int2;
int2.ary[1] = 2;

buffer_t< int, (sizeof( int ) * 1) + 1 > int1p1;

std::cout << sizeof( int2 ) << '\n';
std::cout << sizeof( int1p1 ) << '\n';
}


> I am also wondering about the following:
> template<typename T>
> struct A
> {
> int a;


This next line can't compile if the substitution T = void is done.

> T b;
> };
>
> struct INGET
> {
> };
>
> Why is A<INGET> legal but not A<void>? Semantically they would be the
> same, right?
>


Its illegal because:

void b;

is illegal, i.e. you cannot declare objects of type void.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
Matt Taylor
Guest
Posts: n/a
 
      09-24-2004
"Rob Williscroft" <(E-Mail Removed)> wrote in message
news:Xns956D7E699DB5ukcoREMOVEfreenetrtw@130.133.1 .4...
> Matt Taylor wrote in news:nQn4d.45958$(E-Mail Removed)
> in comp.lang.c++:

[...]
> Its illegal because:
>
> void b;
>
> is illegal, i.e. you cannot declare objects of type void.


Yeah, I know. I was mostly wondering the logic behind why one can create and
instantiate a structure with length 0 but not an object of type void since
they seem to me to be the same idea. This is very strange because you can
create a typedef which is just an alias for void, but you can't use void
itself.

Anyhow, thank you for your reply -- it has helped greatly.

-Matt


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-24-2004

"Matt Taylor" <com.sisecure@mtaylor> wrote in message news:gf05d.54657$(E-Mail Removed) ...

>
> Yeah, I know. I was mostly wondering the logic behind why one can create and
> instantiate a structure with length 0 but not an object of type void since
> they seem to me to be the same idea.


You can't create a structure of size 0. You can create a structure with no data
members in it, but it will still have size greater than zero.

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      09-26-2004
"Matt Taylor" <com.sisecure@mtaylor> wrote:
> I am trying to use templates to create an optimal structure for a fixed-size
> buffer that holds a homogenous array of elements. For brevity, this problem
> is somewhat simplified from my implementation:
>
> template<typename T1, size_t len>
> struct buffer_t
> {
> // Array of elements
> T1 ary[len / sizeof(T1)];
>
> // Align upward to a multiple of sizeof(T1)
> char pad[len - ((len / sizeof(T1)) * sizeof(T1))];
> };
>
> The problem is that, when len is a multiple of sizeof(T1), the
> pad variable has length 0.


Do you need to be able to reference 'pad' ? If not then you
could try:

template<typename T1, size_t len>
union buffer_t
{
T1 ary[len / sizeof(T1)];
char pad[len];
};

(If you need other data members in buffer_t then you would
have to make it a struct, and have a member union).
 
Reply With Quote
 
Matt Taylor
Guest
Posts: n/a
 
      09-28-2004
"Old Wolf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "Matt Taylor" <com.sisecure@mtaylor> wrote:

[...]
> > The problem is that, when len is a multiple of sizeof(T1), the
> > pad variable has length 0.

>
> Do you need to be able to reference 'pad' ? If not then you
> could try:
>
> template<typename T1, size_t len>
> union buffer_t
> {
> T1 ary[len / sizeof(T1)];
> char pad[len];
> };


Ah, I had not thought of this. This is a very simple and intuitive way to
make it work.

My thanks and appreciation to both posters.

-Matt


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
skipping template parameters inside template parameters kito C++ 2 09-26-2010 12:04 AM
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments