Boost logo

Boost :

From: Marco Costalba (mcostalba_at_[hidden])
Date: 2008-05-21 03:15:29


On Wed, May 21, 2008 at 8:37 AM, Lars Schouw <schouwla_at_[hidden]> wrote:
> I tried to use boost::tuple for the first time, and soon ran into a problem.
>
> I can't get values using boost::get<i> where i is an int.
>
> samplec code
>
> typedef boost::tuple<double,double,double> tuple;
>
> void foo()
> {
> std::vector<tuple> corr = boost::assign::tuple_list_of ( 1.0, 0.1, -0.1 ) ( 0.1, 1.0, 0.4 ) ( -0.1, 0.4, 1.0 );
>
> for (int i = 0; i < 3; i++)
> for (int j = 0; j < 3; j++)
> {
> double val = boost::get<i>( corr[j] );
> }
> }
>
> Using Visual Studio 2005 this comes give me a compiler error:
> error C2971: 'boost::tuples::get' : template parameter 'N' : 'i' : a local variable cannot be used as a non-type argument
>
> then when I move the int i declaration outside of the function foo. I get a new error:
> error C2975: 'N' : invalid template argument for 'boost::tuples::get', expected compile-time constant expression
>
> What do I need to do to access values in a tuple with an index i?
>

Error message is quite clear: "expected compile-time constant expression"

Point is that template parameters cannot be variables, 'i' in your example.

BTW given that in the tuple you store values of *same type* why don't
you use a simple array?

typedef double Doubles[3]
Doubles corr[3];

... strip initializing part ...

for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
        {
            double val = corr[j][i];
        }
}


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