473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Templates with constant array parameter

Hi,

How do I parameterize a template by a a allocated array of integers,
which is declared static and constant, so I can make compile time
optimizations depending on the content of the array. The way I have
written my code makes g++ complain about it not being static.

BTW I am new to the C++ language and also the templates it provides.

Here is an example of what I want to do

template<bool condition, class Then, class Else>
struct IF {
typedef Then RET;
};

template<class Then, class Else>
struct IF<false, Then, Else> {
typedef Else RET;
};

struct A {
static void exec () {
cout << "yes" << endl;
}
};

struct B {
static void exec () {
cout << "no" << endl;
}
};

template<const int mask[]>
void foo () {
IF<mask[0] == 1,
A,
B>::RET::exec ();
}

struct Run {
static void run () {
static const int filter[] = {1,1,1};

foo<filter>();
}
};

int main () {
Run::run();

return 0;
}

I want the compiled code just to contain the instructions for

cout << "yes" << endl;

But as I said g++ complains and says

error: non-constant `((*&filter) == 1)' cannot be used as template
argument

The error is from the statement

IF<mask[0] == 1,
A,
B>::RET::exec ();

Hope their is someone who can tell me what I am doing wrong or if is
even possible make compiletime optimizations using arrays.

Best regards,
Kristian Bisgaard Lassen

Jul 22 '05 #1
12 2704
Hi,

I have found out that the modifier const on int* only promises that the
reference does not change. What it points to is not constant. Can you
make some sort of declearation so an array ints = {1,1,1} is constant
also on the content level? Ie. ints is statically an array of {1,1,1}.
That is basically what I need to make the example work.

An alternativ solution would be to use a pair, where the head of each
pair is an constant integer. But I would really like to use an array
somehow since it is a very convenient whay to specify a dataset.

Best regards,
Kristian Bisgaard Lassen

Jul 22 '05 #2
Hi,

I have found out that the modifier const on int* only promises that the
reference does not change. What it points to is not constant. Can you
make some sort of declearation so an array ints = {1,1,1} is constant
also on the content level? Ie. ints is statically an array of {1,1,1}.
That is basically what I need to make the example work.

An alternativ solution would be to use a pair, where the head of each
pair is an constant integer. But I would really like to use an array
somehow since it is a very convenient whay to specify a dataset.

Best regards,
Kristian Bisgaard Lassen

Jul 22 '05 #3
Kristian Bisgaard Lassen wrote:
How do I parameterize a template by a a allocated array of integers,
which is declared static and constant, so I can make compile time
optimizations depending on the content of the array. The way I have
written my code makes g++ complain about it not being static.


The value of a boolean expression used as a tempate argument must be
determined at compile time. Even when enough information is available
to determine the value (as in the example code you posted), the compiler
may fail to do so. Sorry to be the bearer of bad news. :(
Jul 22 '05 #4
Kristian Bisgaard Lassen wrote:
How do I parameterize a template by a a allocated array of integers,
which is declared static and constant, so I can make compile time
optimizations depending on the content of the array. The way I have
written my code makes g++ complain about it not being static.


The value of a boolean expression used as a tempate argument must be
determined at compile time. Even when enough information is available
to determine the value (as in the example code you posted), the compiler
may fail to do so. Sorry to be the bearer of bad news. :(
Jul 22 '05 #5
Kristian Bisgaard Lassen wrote:
Hi,

I have found out that the modifier const on int* only promises that the
reference does not change. What it points to is not constant. Can you
make some sort of declearation so an array ints = {1,1,1} is constant
also on the content level? Ie. ints is statically an array of {1,1,1}.
That is basically what I need to make the example work.


int const * const; // constant pointer to constant int
int const *; // pointer to constant int
int * const; // constant pointer to int.
int *; // pointer to int
Jul 22 '05 #6
Kristian Bisgaard Lassen wrote:
Hi,

I have found out that the modifier const on int* only promises that the
reference does not change. What it points to is not constant. Can you
make some sort of declearation so an array ints = {1,1,1} is constant
also on the content level? Ie. ints is statically an array of {1,1,1}.
That is basically what I need to make the example work.


int const * const; // constant pointer to constant int
int const *; // pointer to constant int
int * const; // constant pointer to int.
int *; // pointer to int
Jul 22 '05 #7
Hi Jeff,
The value of a boolean expression used as a tempate argument must be
determined at compile time.
Yes that is also what I want.
Even when enough information is available
to determine the value (as in the example code you posted), the compiler
may fail to do so.


Why is that? Is it my C++ compiler, g++? Or is it because I have not
written the code so the compiler recognises it as being static? Ie. I
have only declared the pointer to be constant.

Best regards,
Kristian Bisgaard Lassen

Jul 22 '05 #8
Hi Jeff,
The value of a boolean expression used as a tempate argument must be
determined at compile time.
Yes that is also what I want.
Even when enough information is available
to determine the value (as in the example code you posted), the compiler
may fail to do so.


Why is that? Is it my C++ compiler, g++? Or is it because I have not
written the code so the compiler recognises it as being static? Ie. I
have only declared the pointer to be constant.

Best regards,
Kristian Bisgaard Lassen

Jul 22 '05 #9
On Wed, 07 Apr 2004 13:14:48 +0200, Kristian Bisgaard Lassen
<kr***@daimi.au.dk> wrote:
Hi,

How do I parameterize a template by a a allocated array of integers,
which is declared static and constant, so I can make compile time
optimizations depending on the content of the array. The way I have
written my code makes g++ complain about it not being static.

BTW I am new to the C++ language and also the templates it provides.
This is quite advanced for someone who is new to the language!
template<const int mask[]>
void foo () {
IF<mask[0] == 1,
A,
B>::RET::exec ();
}
As you've seen, that's illegal - performing an array lookup isn't a
compile time operation. The contents of arrays (const literal or not)
are never known to the compiler at compile time except where the array
is initialised.
I want the compiled code just to contain the instructions for

cout << "yes" << endl;
That is possible, using recursive lists of integers rather than
arrays. Two versions (both tested with GCC and Comeau C++) below:

#include <iostream>
using namespace std;

template<bool condition, class Then, class Else>
struct IF {
typedef Then RET;
};

template<class Then, class Else>
struct IF<false, Then, Else> {
typedef Else RET;
};

struct A {
static void exec () {
cout << "yes" << endl;
}
};

struct B {
static void exec () {
cout << "no" << endl;
}
};

template <int i, class Tail>
struct IntList
{
static int const value = i;
typedef Tail tail;
};

struct null_type{};

template <int i, class List>
struct AtIndex
{
typedef typename AtIndex<i - 1, typename List::tail>::type type;
};

template <class List>
struct AtIndex<0, List>
{
typedef List type;
};

template<class IntList>
void foo () {
IF<AtIndex<0, IntList>::type::value == 1,
A,
B>::RET::exec ();
}

struct Run {
static void run () {
foo<IntList<1, IntList<1, IntList<1, null_type> > > >();
}
};

int main () {
Run::run();

return 0;
}
Or using boost's MPL library (www.boost.org):

#include <boost/mpl/if.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/at.hpp>
using namespace boost::mpl;

#include <iostream>
using namespace std;

struct A {
static void exec () {
cout << "yes" << endl;
}
};

struct B {
static void exec () {
cout << "no" << endl;
}
};

template<class List>
void foo () {
if_c<
at_c<List, 0>::type::value == 1,
A,
B::type::exec();

}

struct Run {
static void run () {
foo<vector_c<int, 1, 1, 1> >();
}
};

int main () {
Run::run();

return 0;
}

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #10
On Wed, 07 Apr 2004 13:14:48 +0200, Kristian Bisgaard Lassen
<kr***@daimi.au.dk> wrote:
Hi,

How do I parameterize a template by a a allocated array of integers,
which is declared static and constant, so I can make compile time
optimizations depending on the content of the array. The way I have
written my code makes g++ complain about it not being static.

BTW I am new to the C++ language and also the templates it provides.
This is quite advanced for someone who is new to the language!
template<const int mask[]>
void foo () {
IF<mask[0] == 1,
A,
B>::RET::exec ();
}
As you've seen, that's illegal - performing an array lookup isn't a
compile time operation. The contents of arrays (const literal or not)
are never known to the compiler at compile time except where the array
is initialised.
I want the compiled code just to contain the instructions for

cout << "yes" << endl;
That is possible, using recursive lists of integers rather than
arrays. Two versions (both tested with GCC and Comeau C++) below:

#include <iostream>
using namespace std;

template<bool condition, class Then, class Else>
struct IF {
typedef Then RET;
};

template<class Then, class Else>
struct IF<false, Then, Else> {
typedef Else RET;
};

struct A {
static void exec () {
cout << "yes" << endl;
}
};

struct B {
static void exec () {
cout << "no" << endl;
}
};

template <int i, class Tail>
struct IntList
{
static int const value = i;
typedef Tail tail;
};

struct null_type{};

template <int i, class List>
struct AtIndex
{
typedef typename AtIndex<i - 1, typename List::tail>::type type;
};

template <class List>
struct AtIndex<0, List>
{
typedef List type;
};

template<class IntList>
void foo () {
IF<AtIndex<0, IntList>::type::value == 1,
A,
B>::RET::exec ();
}

struct Run {
static void run () {
foo<IntList<1, IntList<1, IntList<1, null_type> > > >();
}
};

int main () {
Run::run();

return 0;
}
Or using boost's MPL library (www.boost.org):

#include <boost/mpl/if.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/at.hpp>
using namespace boost::mpl;

#include <iostream>
using namespace std;

struct A {
static void exec () {
cout << "yes" << endl;
}
};

struct B {
static void exec () {
cout << "no" << endl;
}
};

template<class List>
void foo () {
if_c<
at_c<List, 0>::type::value == 1,
A,
B::type::exec();

}

struct Run {
static void run () {
foo<vector_c<int, 1, 1, 1> >();
}
};

int main () {
Run::run();

return 0;
}

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #11
Kristian Bisgaard Lassen wrote:
Hi Jeff,
The value of a boolean expression used as a tempate argument must be
determined at compile time.

Yes that is also what I want.
Even when enough information is available to determine the value (as
in the example code you posted), the compiler may fail to do so.

Why is that? Is it my C++ compiler, g++? Or is it because I have not
written the code so the compiler recognises it as being static? Ie. I
have only declared the pointer to be constant.


The post from tom_usenet on this thread explains quite eloquently. :)

If you want to get into metaprogramming, check out the Josuttis book on
templates. It was an eye opener for me.
Jul 22 '05 #12
Kristian Bisgaard Lassen wrote:
Hi Jeff,
The value of a boolean expression used as a tempate argument must be
determined at compile time.

Yes that is also what I want.
Even when enough information is available to determine the value (as
in the example code you posted), the compiler may fail to do so.

Why is that? Is it my C++ compiler, g++? Or is it because I have not
written the code so the compiler recognises it as being static? Ie. I
have only declared the pointer to be constant.


The post from tom_usenet on this thread explains quite eloquently. :)

If you want to get into metaprogramming, check out the Josuttis book on
templates. It was an eye opener for me.
Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Kristian Bisgaard Lassen | last post by:
Hi, How do I parameterize a template by a a allocated array of integers, which is declared static and constant, so I can make compile time optimizations depending on the content of the array....
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
7
by: icosahedron | last post by:
Is there a way to determine if a parameter to a function is a constant (e.g. 2.0f) versus a variable? Is there some way to determine if this is the case? (Say some metaprogramming tip or type...
4
by: John Goche | last post by:
Hello, I was wondering what the pros and cons are for using numbers in templates rather than in constructors as in: MyBuffer<10foo; rather than MyBuffer foo(10);
8
by: Malciah | last post by:
I posted this on another site, but so far I've had no answers. So, I decided to try it here. -------------------------------------------------------- I've been learning C++ for about 6 weeks now,...
8
by: coder_lol | last post by:
I put together a quick array template for MS Visual Studio 2003, but I ran into some trouble. It's been quite a while since I had to roll my own templates, so I'd appreciate all help. Example 1...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.