473,805 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array template parameters

Please have a look at the following program:

#include <iostream>

template <const int array[], size_t index>
class ArrayIndex
{
public:
static const int value = array[index];
};

extern const int array[] = { 1, 2, 3, 4, 5 };

int main()
{
std::cout <<
ArrayIndex<arra y, 0>::value << "," <<
ArrayIndex<arra y, 1>::value << "," <<
ArrayIndex<arra y, 2>::value << "," <<
ArrayIndex<arra y, 3>::value << "," <<
ArrayIndex<arra y, 4>::value;
}

Output is, on my C++ compiler (VS 2003): 0,0,0,0,0 while I'd expect that the
output is 1,2,3,4,5. What actually does the C++ Standard say about this? In
other words, is the above program correct and if it is, what should be its
output with a standard-compliant compiler ? Moreover, if the program is
correct, is the expression 'array[index]' within the ArrayIndex class
regarded as a constant expression ?

(note that I had to declare the array with 'extern', which shouldn't be
necessary by the Standard, as this array has external linkage, anyway.
However, the compiler issues an error if I don't do this. So I have reasons
to suspect that the compiler is not standard-compliant).

Regards,
Rade

Jul 22 '05 #1
7 3661
template <const int array[], size_t index>
class ArrayIndex
{
public:
static const int value;
};

template <const int array[], size_t index>
const int ArrayIndex<arra y, index>::value = array[index];
-----------------------------------------------------------------
Static member definition should be separated from its declaration.

Jul 22 '05 #2
"serock" <s.******@gmail .com> wrote in message
template <const int array[], size_t index>
class ArrayIndex
{
public:
static const int value;
};

template <const int array[], size_t index>
const int ArrayIndex<arra y, index>::value = array[index];
-----------------------------------------------------------------
Static member definition should be separated from its declaration.


This is not required for static const integral types (int, char, bool, long,
short). I think the OP's code is legal, though my version of g++ and
Borland both complain.
Jul 22 '05 #3
Siemel Naran wrote in
news:oZ******** ***********@bgt nsc05-news.ops.worldn et.att.net in
comp.lang.c++:
"serock" <s.******@gmail .com> wrote in message
template <const int array[], size_t index>
class ArrayIndex
{
public:
static const int value;
};

template <const int array[], size_t index>
const int ArrayIndex<arra y, index>::value = array[index];
-----------------------------------------------------------------
Static member definition should be separated from its declaration.


This is not required for static const integral types (int, char, bool,
long, short). I think the OP's code is legal, though my version of
g++ and Borland both complain.


The problem is that the initializer "array[ index ]" *isn't* a
compile time constant (intergral constant expression).

IOW, the out of class initialization is required.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
"Rob Williscroft" <rt*@freenet.co .uk> wrote in message
Siemel Naran wrote in

This is not required for static const integral types (int, char, bool,
long, short). I think the OP's code is legal, though my version of
g++ and Borland both complain.


The problem is that the initializer "array[ index ]" *isn't* a
compile time constant (intergral constant expression).

IOW, the out of class initialization is required.


Do you have a quote from the standard? I agree that if if 'array' has type
"int *" then array[3] is not an integral constant, but if it has type "int
const *" then array[3] should be a constant so long as array itself is
constant, which is the case because it is extern const.

In any case, I made the initialization out of line for g++ to compile it,
and I still witness the original behavior that it prints "0,0,0,0,0, "
instead of "1,2,3,4,5, ". Strange. Yet when I force the instantiation of
the specific classes by using template class, it does work. Stranger.
Jul 22 '05 #5
"Rade" <no************ *@btinternet.co m> wrote in message news:covce2$e84
#include <iostream>

template <const int array[], size_t index>
class ArrayIndex
{
public:
static const int value = array[index];
};

extern const int array[] = { 1, 2, 3, 4, 5 };

int main()
{
std::cout <<
ArrayIndex<arra y, 0>::value << "," <<
ArrayIndex<arra y, 1>::value << "," <<
ArrayIndex<arra y, 2>::value << "," <<
ArrayIndex<arra y, 3>::value << "," <<
ArrayIndex<arra y, 4>::value;
}

Output is, on my C++ compiler (VS 2003): 0,0,0,0,0 while I'd expect that the output is 1,2,3,4,5.
This is quite strange. I added statements to force the compiler to
instantiate the ArraynIndex classes, and it works now, at least on g++
2.95.2-6. Add the five lines (without >) to your program and see if it
works.
extern const int array[] = { 1, 2, 3, 4, 5 };
template class ArrayIndex<arra y, 0>;
template class ArrayIndex<arra y, 1>;
template class ArrayIndex<arra y, 2>;
template class ArrayIndex<arra y, 3>;
template class ArrayIndex<arra y, 4>;

int main()
What actually does the C++ Standard say about this? In
other words, is the above program correct and if it is, what should be its
output with a standard-compliant compiler ? Moreover, if the program is
correct, is the expression 'array[index]' within the ArrayIndex class
regarded as a constant expression ?
Seems to me array[index] is an integral expression because array has type T
const *const, but don't know what the standard says.
(note that I had to declare the array with 'extern', which shouldn't be
necessary by the Standard, as this array has external linkage, anyway.
However, the compiler issues an error if I don't do this. So I have reasons to suspect that the compiler is not standard-compliant).


Const objects have internal linkage by default, so the extern is necessary.
Jul 22 '05 #6
> Static member definition should be separated from its declaration.

OK, it's better now, but does C++ language require it, or the requirement
comes from Microsoft ?

As far as I know, one should be able to define static consts inside the
class as well as outside, as long as they are initialized by a constant
expression. That's why I asked about whether array[index] is regarded as a
constant expression in this context. If it is constant, then my code should
work (but it doesn't). If it is not constant, then the compiler should have
warned me that I was trying to initialize a static const within a class with
a nonconstant expression (but it hasn't). Am I right ?

Rade
Jul 22 '05 #7
Siemel Naran wrote in
news:Z7******** ***********@bgt nsc05-news.ops.worldn et.att.net in
comp.lang.c++:
"Rob Williscroft" <rt*@freenet.co .uk> wrote in message
Siemel Naran wrote in

> This is not required for static const integral types (int, char,
> bool, long, short). I think the OP's code is legal, though my
> version of g++ and Borland both complain.


The problem is that the initializer "array[ index ]" *isn't* a
compile time constant (intergral constant expression).

IOW, the out of class initialization is required.


Do you have a quote from the standard?


5.19 & 5.2.1

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #8

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

Similar topics

14
3920
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
10
10298
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. Returning a pointer to the first element might do but I want to do what I've said. Fraser.
12
3721
by: prashna | last post by:
Hi Guru's, Here are my questions... 1)Why does c allows an extra "," in array intialiser?Is there any advantage of this? ex: int arr={1,2,3,4,5,}; ^^Compiler does not give error for this! 2)How to determine the size of the array which is passes as a
3
2699
by: danbraund | last post by:
Hi everyone, I'm a long time C coder, who is coding his final year project in C++ to run under the MIT click routing system. Being fairly new to the OO side of the language, my problem is this: In C++, how can I define a global array whose size is determined by parameters passed to a class? The array is of static size once defined, but the parameters of course arent read until the config function is called at class startup. is there a...
3
1983
by: kk_oop | last post by:
Hi. I recently wrote a simple little template that defines an array that checks attempts to use out of bounds indexes. The only problem is that it does provide the use array style value initialization when the type is instantiated. Any suggestions for a mod that would allow array initialization syntax? ***********Here's the type: #ifndef CHECKED_ARRAY_ #define CHECKED_ARRAY_
12
2284
by: Angel Tsankov | last post by:
I'm thinking about using a function tamplate to verify that an identifier is of an array type. May the following template be used for this purpose: template< typename T, int N > void check_array( T(&) ) { } And what about using size_t instead of int?
8
1542
by: Wayne Shu | last post by:
Why there has such a rule for the template non-type argument?? but address of an object of external linkage is acceptable!! e.g. template <int *p> class C; int i; int arr;
3
2429
by: tomerdr | last post by:
Hi, I have been asked to create a dynamic template array as an exercise. the array should be used as follows: CDynamicArray<City*arr; or CDynamicArray<Streetarr; It also need to support operator << for streaming the content. (each class also support operator << ) My question is how do i deal with the fact that the array can contains
7
2952
by: Christof Warlich | last post by:
Hi, the subject says it all: I need to instantiate an array of objects where each object "knows" its arrary index. So far, this is easy as long as index is not a compile-time constant: class Elements { public: Elements(): index(maxIndex) {
0
9718
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10613
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10368
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.