473,396 Members | 1,843 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,396 software developers and data experts.

Elements of constant array as template arguments.

Hi!

I cannot figure out, why is the following code incorrect:

const int array[] = { 1 };
const int var = 1;

template<int I>
struct Blah {};

int main() {
Blah<var> b1;
Blah<array[0]> b2; // ERROR!
}

I know that template arguments can only be constant expressions. But I don't
understand why isn't 'array[0]' treated as a constant expression. It seems
to me that if an array is const and the index is a constant expression
(e.g. 0), then the whole expression is constant. I can't imagine, how a
compiler can NOT know, what's the value of array[0] during compile-time.

I would be grateful, if you explained me this issue.

Thanks in advance! (I hope, you understand me :))

--
Pozdrawiam, Andrzej
May 2 '06 #1
1 3047
Andrzej 'Foxy' D. wrote:
I cannot figure out, why is the following code incorrect:

const int array[] = { 1 };
const int var = 1;

template<int I>
struct Blah {};

int main() {
Blah<var> b1;
Blah<array[0]> b2; // ERROR!
}

I know that template arguments can only be constant expressions. But
I don't understand why isn't 'array[0]' treated as a constant
expression.
Indexing is a run-time thing. 'array' is converted to a pointer and
then indexing is performed on that pointer. It does not qualify as
a compile-time constant expression which is defined in subclause 5.19.
Pointers just cannot be part of a constant expression except as
arguments of 'sizeof'.
It seems to me that if an array is const and the index is
a constant expression (e.g. 0), then the whole expression is
constant. I can't imagine, how a compiler can NOT know, what's the
value of array[0] during compile-time.


The langauge just doesn't specify it that way. As to why it is so,
ask in 'comp.std.c++', they could explain the rationale behind
different parts of the Standard.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 2 '06 #2

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....
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
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...
8
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;
4
by: j_depp_99 | last post by:
Thanks to those guys who helped me out yesterday. I have one more problem; my print function for the queue program doesnt work and goes into an endless loop. Also I am unable to calculate the...
3
by: iglpdc | last post by:
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...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
2
by: Pavan | last post by:
Hi, I need to create a consant array of larze size but however its elements can be calculated using an equation, say for example I need an int arry of 20 elements where each element will be arr...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.