non static integral const template arguments

F

forums_mp

Consider:

# include <vector>
# include <iostream>
# include <string>
template < typename UnsignedType, unsigned b, unsigned o >
class controller
{
UnsignedType& a ;
// stuff
public :
controller ( UnsignedType a_ )
: a( a_)
{}
};

struct sequence_of {

unsigned int mValue ;
//enum { base_addr, moffset } ;
unsigned int const base_addr ;
unsigned int const moffset;
controller < unsigned int, base_addr, moffset > mwhatever;
sequence_of ( unsigned int base_addr_, unsigned int offset_ )
: mValue ( 0 )
, base_addr ( base_addr_ )
, moffset ( offset_ )
, mwhatever ( mValue )
{}
};

typedef std::vector < sequence_of* > SEQOF_VEC ;

int main() {

controller < unsigned int, 3, 4 > a ( 0 ) ; //ok

int offset = 0 ;
SEQOF_VEC mSeqVec ;
int const base_addr = 0x900000 ;
for ( int odx ( 0 ); odx < 30 ; ++odx ) {
mSeqVec.push_back ( new sequence_of ( base_addr, offset ) ) ;
offset += 2 ;
}

}


I'd like to create an array of the struct sequence_of as shown in
main. At issue: I'm not able to specify the second and third
parameters in the template argument list for the controller class
within sequence_of without making them static integral constants. Is
there an alternate solution to achieving my objective short of
changing the controller class?
 
A

Alf P. Steinbach

* (e-mail address removed):
Consider:

# include <vector>
# include <iostream>
# include <string>
template < typename UnsignedType, unsigned b, unsigned o >
class controller
{
UnsignedType& a ;
// stuff
public :
controller ( UnsignedType a_ )
: a( a_)
{}
};

struct sequence_of {

unsigned int mValue ;
//enum { base_addr, moffset } ;
unsigned int const base_addr ;
unsigned int const moffset;
controller < unsigned int, base_addr, moffset > mwhatever;

You cannot specify a compile time constant at run time.

sequence_of ( unsigned int base_addr_, unsigned int offset_ )
: mValue ( 0 )
, base_addr ( base_addr_ )
, moffset ( offset_ )
, mwhatever ( mValue )
{}
};

typedef std::vector < sequence_of* > SEQOF_VEC ;

Preferentially use all uppercase names for macros and macros only.

An all uppercase name is otherwise an abomination, except for idiomatic usage.

int main() {

controller < unsigned int, 3, 4 > a ( 0 ) ; //ok

int offset = 0 ;
SEQOF_VEC mSeqVec ;
int const base_addr = 0x900000 ;
for ( int odx ( 0 ); odx < 30 ; ++odx ) {
mSeqVec.push_back ( new sequence_of ( base_addr, offset ) ) ;
offset += 2 ;
}

}

Why use a vector of pointers instead of a vector of sequence_of objects.

I'd like to create an array of the struct sequence_of as shown in
main. At issue: I'm not able to specify the second and third
parameters in the template argument list for the controller class
within sequence_of without making them static integral constants. Is
there an alternate solution to achieving my objective short of
changing the controller class?

Assuming that by "my objective" you mean "that objective", no.


Cheers & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top