Boost logo

Boost :

From: Vincent Bherer-Roy (vbr.boost_at_[hidden])
Date: 2006-05-24 12:12:04


I know that this subject has been discuss a lot in the past. However a quick
search in the mailing list archive did not show any solution like the one I
propose. If my post is a duplicate, please forgive :-)

The problem is that, like many other, I don't use boost::array because it
doesn't have any constructor.

First, be aware that I want to start a flame war ;-) I understand well the
rationale for the aggregate initialization support. I also understand that
we want a lightweight wrapper around C arrays. I agree there is a consensus
around this and I don't want to break it. However, I have a simple (I think)
solution that should give us the best of both worlds.

Basically, the idea is to have a third parameter to the template, it is set
to false by default in order to provide the same default behaviour. So we
have:

template<class T, std::size_t N, bool C = false> class array { /* current
implementation */ };
template<class T, std::size_t N> class array<T, N, true> : public
array<T,N,false> { /* adds constructors only */ };

   array<int, 3> a1 = { 1, 2, 3 };
   array<int, 3, false> a2 = { 1, 2, 3 };
   array<int, 3, true> a3;
   array<int, 3, true> a4( 1 ); // assign 1 to all three values
   array<int, 3, true> a5( 1, 2, 3 );
   array<int, 3, true> a6( 1, 2 ); // raises a static assert because we try
to put only two values in an array of 3
   array<int, 3, true> a7( 1, 2, 3, 4 ); // also raises a static assert
because we try to put four values in an array of 3

I only see one main drawback with this technique, we'll have some problems
with compilers lacking partial specialization support.

So the idea is in your hands. I hope it will have some echoes.

I attached the modified array.hpp file. It compiles for me on Visual Studio
.Net 2003 and 2005 with boost 1.33.1. However I didn't make full test
coverage.

Thanks

Vincent Bherer-Roy




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk