473,387 Members | 1,465 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,387 software developers and data experts.

Problems with void Template Type Parameters and Zero-Length Arrays

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
Jul 22 '05 #1
5 4457
Matt Taylor wrote in news:nQ*******************@tornado.tampabay.rr.com
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::pad;
};
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/
Jul 22 '05 #2
"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn*********************************@130.133.1 .4...
Matt Taylor wrote in news:nQ*******************@tornado.tampabay.rr.com
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
Jul 22 '05 #3

"Matt Taylor" <com.sisecure@mtaylor> wrote in message news:gf*******************@tornado.tampabay.rr.com ...

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.

Jul 22 '05 #4
"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).
Jul 22 '05 #5
"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:84**************************@posting.google.c 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
Jul 22 '05 #6

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

Similar topics

2
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for...
1
by: Eric | last post by:
I am trying to figure out a good way to implement a XSLT transformation. Basically my goal is that I want to be able to ouput the following XML in a document: <chart type="pie" width="100"...
2
by: Stephen Starkie | last post by:
Hi, For a while I have had some problem understanding just how template specialisation works in certain cases. In abridged form my code looks like this; --MyTemplate.h-- #ifndef MyTemplateH...
0
by: Claire | last post by:
Hi Ive been using Mattias Sjögren's example at http://www.msjogren.net/dotnet/eng/samples/dotnet_dynpinvoke.asp to load an unmanaged 3rd party dll dynamically when my object is created. Calling...
1
by: =?UTF-8?B?SmVucyBNw7xsbGVy?= | last post by:
(I also posted this to boost-user) The BGL implementation of breadth-first search uses a dedicated color map. I had the following idea: Some algorithms don't need to distinguish black/gray,...
2
by: desktop | last post by:
I have this code from the book C++ Templates: The Complete Guide: #ifndef ACCUM8_HPP_ #define ACCUM8_HPP_ #include "accumtraits4.hpp" #include "sumpolicy2.hpp"
2
by: renagade629 | last post by:
Can anybody help me understand what i'm doing wrong or what I'm missing? Is there anyother good and commendable C++ program I can use (free) from the internet like Dev C++? I'm having trouble doing...
3
by: Adrian | last post by:
In the following code example I am trying to create a generic interface for a bunch of objects. Each concrete type is stored in its own container and the a container of pointer's to base is used so I...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.