Template, fixed sized array, assignment

Feb 10, 2009 at 10:26am
Hello Programmers,

I'm trying to get a simple class (called Buffer) to work but I'm having some troubles.

What I want is to be able to assign my class a fixed size array in a single line. Currently I have to write

1
2
Buffer::byte_t const Data[] = {1, 2, 3};
Buffer MyBuffer(sizeof(Data), Data);


This works just fine, but it's pretty annoying.

So I tried something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Buffer {

	// ...

	typedef unsigned char byte_t;

	// ...

	template<int T>
	Buffer & Buffer::operator=(byte_t const Data[T]) {
		this->Assign(T, Data);
		return *this;
	}

	// ...
};


This compiles without errors/warnings with VS2008. The problem is that the operator isn't recognized.
When I try this:

1
2
3
Buffer::byte_t const Data[] = {1, 2, 3};
Buffer MyBuffer;
MyBuffer = Data;


I get
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const Buffer::byte_t [3]' (or there is no acceptable conversion)

Does anyone know hot to solve the problem? What I want is to initialize/assign the buffer with an fixed size array without the need of an additional variable.

thx,
Korexio
Feb 10, 2009 at 10:53am
This is a specialisation vs overloading issue.

See: http://www.ddj.com/cpp/184401413
Feb 10, 2009 at 1:11pm
Hello kbw,

thanks for the link to the really interesting article. But I don't find a relation between the article and my problem.

My problem seems to be that an array is always a pointer, never a own type like Buffer::byte_t [3] .

The following code compiles just fine, although it seems to be unsafe:

1
2
3
4
5
6
7
8
9
10
11
12
13
template<int T>
void f(long Data[T]) {
}

void g(long Data[2]) {
}

int main(void) {
long Data[] = {1, 2, 3};
f<1>(Data);
f<2>(Data);
f<3>(Data);
g(Data);


What I wanted t do is to somehow determine the size of an fixed size array at compile time, generate a function from the template and avoid passing the size of the array explicitly to the function. The template parameter would have to be the size of the array.

What I wonder is why the compiler mentions const Buffer::byte_t [3] anyway, shouldn't it be Buffer::byte_t const *?
Feb 10, 2009 at 2:21pm
boost::array<6> array = { 1, 2, 3, 4, 5, 6 };

is legal syntax and compiles/runs fine. Try using boost::array.


Feb 10, 2009 at 3:11pm
Hello jsmith,

Thanks for the hint, that was just what I'm looking for.

boost solves the problem by providing a class for each array size (via a template class). As far as I understand, every time I initialize an array with a given size, a new class is deduced from the template. Therefore I'll get many different classes, although only the (initial) size varies. That's not exactly what I'm looking for, but it serves the purpose.

Your code does not compile on my machine though:
1
2
3
4
5
6
7
8
9
10

#include <boost/array.hpp>

int main(void) {

	boost::array<6> array = { 1, 2, 3, 4, 5, 6 };
	return 0;

}


gives me the following errors (but it clears your point):

error C2976: 'boost::array' : too few template arguments
error C2552: 'array' : non-aggregates cannot be initialized with initializer list

You can not omit the T template parameter, only the N (size of the array defaults to 0) template parameter.

Still I have to provide the size of the array by myself . . .
It seems as if it's not possible to let the compiler (or preprocessor) fill in the numbers.

Thanks a lot,
Korexio

Topic archived. No new replies allowed.