Boost logo

Boost Users :

Subject: [Boost-users] Tuples: Can someone help me generalize this?
From: Derrick Hathaway (d.hathaway_at_[hidden])
Date: 2008-09-10 11:44:28


Hello, boost-users,
what I am trying to accomplish is a generic collection of N objects of
type Type. I need for the N objects to be tightly packed and I would
like the collection to be constructible in this way:

uniform_vector<3, float> my_point(1.0f, 2.1f, 3.2f);

I'm very new to the template (meta)programming scene and I would like
to get better at it, so any advice you could give that would be
instructive in that regard would be much appreciated. The code below
is what I have come up with.

Question 1: Am I reinventing the wheel at all? In other words is there
something I can just typedef to get this functionality?

Question 2: If not, how could I generalize this? Is there a template
metaprogramming way? I'm sure it can be done with the preprocessor but
I'm no good with macros... but then again I'm not much good with
templates either at least not by a boost mailing list standard
anyway. :)

Thanks, Derrick.

// -- uniform_vector.hpp
------------------------------------------------------------------------
#ifndef DJH_UNIFORM_VECTOR_HPP
#define DJH_UNIFORM_VECTOR_HPP

#include <boost/tuple/tuple.hpp>

namespace djh {
        template <unsigned int N, class Type>
        struct uniform_vector;

        template <class Type>
        struct uniform_vector<0, Type>;
        
        template <class Type>
        struct uniform_vector<1, Type>
        {
                static const unsigned int size = 1;
                typedef boost::tuple<Type> type;
        };
        
        template <class Type>
        struct uniform_vector<2, Type>
        {
                static const unsigned int size = 2;
                typedef boost::tuple<Type, Type> type;
        };
        
        template <class Type>
        struct uniform_vector<3, Type>
        {
                static const unsigned int size = 3;
                typedef boost::tuple<Type, Type, Type> type;
        };
        
        template <class Type>
        struct uniform_vector<4, Type>
        {
                static const unsigned int size = 4;
                typedef boost::tuple<Type, Type, Type, Type> type;
        };
        
}

#endif



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net