By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,002 Members | 2,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,002 IT Pros & Developers. It's quick & easy.

Using array variables as a non-type template arguments

P: n/a
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_opera;

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!
Nov 19 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
iglpdc wrote:
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_opera;

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 20 '07 #2

P: n/a
LR
iglpdc wrote:
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_opera;

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_operAType;
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.

Nov 20 '07 #3

P: n/a
On 19 nov, 20:06, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
iglpdc wrote:
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_opera;
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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.
Nov 20 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.