Using array variables as a non-type template arguments

I

iglpdc

Hi,

I need a template class taking several non-type arguments. Then I want
to be able to pass these arguments to the constructor of another non-
template class. So far I have this (I did some editing to simplify the
thing):

template<int const* rule_, double const* raw_oper_>
struct A{

static const int* rule() { return rule_; }
static const double* raw_oper() {return raw_oper_; }
};

I have the arrays defined with external linkage:

extern int const _rule[]={ 0, 1 };
extern double const _raw_oper[]={ 0.0, 1.0 };

A<_rule, _raw_oper> a;

struct B{

B(const int* rule, const double * raw_oper) { // create B with the
arrays };
// more stuff here
};

So far it's fine (I guess). This is pretty much the example on how to
use char const* arguments in page 110 of Vandevoorde & Josuttis book.
However if I do:

B b(a::rule(), a::raw_oper());

The compiler complains with something like:

operators.h:522: error: no matching function for call to 'B::B(const
double* (&)(), const int*)'
operators.h:366: note: candidates are: B::B(const double*, const
int*)

Why is there a difference in the way the array of int and the array of
double
are treated? How can I return the array variables to use them in other
parts of the code?

Many thanks in advance!
 
V

Victor Bazarov

iglpdc said:
Hi,

I need a template class taking several non-type arguments. Then I want
to be able to pass these arguments to the constructor of another non-
template class. So far I have this (I did some editing to simplify the
thing):

template<int const* rule_, double const* raw_oper_>
struct A{

static const int* rule() { return rule_; }
static const double* raw_oper() {return raw_oper_; }
};

I have the arrays defined with external linkage:

extern int const _rule[]={ 0, 1 };
extern double const _raw_oper[]={ 0.0, 1.0 };

A<_rule, _raw_oper> a;

struct B{

B(const int* rule, const double * raw_oper) { // create B with the
arrays };
// more stuff here
};

So far it's fine (I guess). This is pretty much the example on how to
use char const* arguments in page 110 of Vandevoorde & Josuttis book.
However if I do:

B b(a::rule(), a::raw_oper());

The compiler complains with something like:

operators.h:522: error: no matching function for call to 'B::B(const
double* (&)(), const int*)'
operators.h:366: note: candidates are: B::B(const double*, const
int*)

Setting aside the reasons for which you're doing it (I haven't
figured out the need for the 'A' template yet), it seems that
you have simply lost the parentheses after the 'raw_oper' in your
code (and you actually supplying them here).

Why is there a difference in the way the array of int and the array of
double
are treated? How can I return the array variables to use them in other
parts of the code?

You made a syntax error. Correct it and everything is going to be OK.

V
 
L

LR

iglpdc said:
template<int const* rule_, double const* raw_oper_>
struct A{

static const int* rule() { return rule_; }
static const double* raw_oper() {return raw_oper_; }
};
extern int const _rule[]={ 0, 1 };
extern double const _raw_oper[]={ 0.0, 1.0 };

I've forgotten the rules, but AFAIR, those leading underscores might not
be good.

A<_rule, _raw_oper> a;

struct B{
B(const int* rule, const double * raw_oper) {}
B b(a::rule(), a::raw_oper());


Shouldn't that be something like:

typedef A<_rule, _raw_oper> AType;
B b(AType::rule(), AType::raw_oper());

or
B b(a.rule(), a.raw_oper());

Why is there a difference in the way the array of int and the array of
double are treated?

They are different types? Perhaps I don't understand the question.




How can I return the array variables to use them in other
parts of the code?

I don't think that you can return arrays. I think you can return
pointers to arrays, or use a std::vector of some sort and return that.
Or you can wrap your array in a struct and return that.
 
I

iglpdc

iglpdc said:
I need a template class taking several non-type arguments. Then I want
to be able to pass these arguments to the constructor of another non-
template class. So far I have this (I did some editing to simplify the
thing):
template<int const* rule_, double const* raw_oper_>
struct A{
static const int* rule() { return rule_; }
static const double* raw_oper() {return raw_oper_; }
};
I have the arrays defined with external linkage:
extern int const _rule[]={ 0, 1 };
extern double const _raw_oper[]={ 0.0, 1.0 };
A<_rule, _raw_oper> a;
struct B{
B(const int* rule, const double * raw_oper) { // create B with the
arrays };
// more stuff here
};
So far it's fine (I guess). This is pretty much the example on how to
use char const* arguments in page 110 of Vandevoorde & Josuttis book.
However if I do:
B b(a::rule(), a::raw_oper());
The compiler complains with something like:
operators.h:522: error: no matching function for call to 'B::B(const
double* (&)(), const int*)'
operators.h:366: note: candidates are: B::B(const double*, const
int*)

Setting aside the reasons for which you're doing it (I haven't
figured out the need for the 'A' template yet), it seems that
you have simply lost the parentheses after the 'raw_oper' in your
code (and you actually supplying them here).
Why is there a difference in the way the array of int and the array of
double
are treated? How can I return the array variables to use them in other
parts of the code?

You made a syntax error. Correct it and everything is going to be OK.

V

You're right! Sorry for such a mistake. I was two days with this :).
I thought it was something related to double not being an integral
type, or some other complicated template stuff... Thanks again.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top