473,888 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

constant-expression

What constitutes a constant-expression? I know that it is something that
can be determined at compile time.

I am trying to use template code and keep getting compiler errors
"error: cannot appear in a constant-expression"

template <int s>
class CFoo
{
private:
int m_val[s];
};

struct SParams
{
const int m_sz;
SParams(int sz) : m_sz(sz) {;}
};

int main(int argc, char *argv[])
{
CFoo<2foo;

const int sz(2);
CFoo<szfoo2;

int sz2(3);
CFoo<sz2foo3;

const SParams params(4);
CFoo<params.m_s zfoo4;
}

When I compile this code I get the following output.

dwhs1@triton:~/test$ g++ -o template template.cpp
template.cpp:22 : error: ‘sz2’ cannot appear in a constant-expression
template.cpp:22 : error: template argument 1 is invalid
template.cpp:22 : error: invalid type in declaration before ‘;’ token
template.cpp: In function ‘int main(int, char**)’:
template.cpp:25 : error: ‘params’ cannot appear in a constant-expression
template.cpp:25 : error: `.' cannot appear in a constant-expression
template.cpp:25 : error: template argument 1 is invalid
template.cpp:25 : error: invalid type in declaration before ‘;’ token

I understand that in line 22 I am using a local variable that is not
const, but on line 25 there is a const member of a const structure.

What's going on here?

thanks

dan
Jun 27 '08 #1
3 4773
Dan Smithers wrote:
What constitutes a constant-expression? I know that it is something
that can be determined at compile time.

I am trying to use template code and keep getting compiler errors
"error: cannot appear in a constant-expression"

template <int s>
class CFoo
{
private:
int m_val[s];
};

struct SParams
{
const int m_sz;
SParams(int sz) : m_sz(sz) {;}
};

int main(int argc, char *argv[])
{
CFoo<2foo;

const int sz(2);
CFoo<szfoo2;

int sz2(3);
CFoo<sz2foo3;

const SParams params(4);
CFoo<params.m_s zfoo4;
}

When I compile this code I get the following output.

dwhs1@triton:~/test$ g++ -o template template.cpp
template.cpp:22 : error: 'sz2' cannot appear in a constant-expression
template.cpp:22 : error: template argument 1 is invalid
template.cpp:22 : error: invalid type in declaration before ';' token
template.cpp: In function 'int main(int, char**)':
template.cpp:25 : error: 'params' cannot appear in a
constant-expression template.cpp:25 : error: `.' cannot appear in a
constant-expression template.cpp:25 : error: template argument 1 is
invalid
template.cpp:25 : error: invalid type in declaration before ';' token

I understand that in line 22 I am using a local variable that is not
const, but on line 25 there is a const member of a const structure.

What's going on here?
I think a function call (and you have a constructor defined in your
'SParams' struct) interferes with the "const-ness" of 'params' object.
Since the initialisation requires a constructor call (never mind that
it actually can be optimized), the object is considered initialised at
run-time, and therefore cannot be part of the compile-time const expr.

That's my take on it, I didn't actually verify with 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
Jun 27 '08 #2
On May 9, 7:28*pm, Dan Smithers <dsmith...@talk talk.netwrote:
What constitutes a constant-expression? I know that it is something that
can be determined at compile time.

I am trying to use template code and keep getting compiler errors
"error: cannot appear in a constant-expression"

template <int s>
class CFoo
{
private:
* int m_val[s];

};

struct SParams
{
* const int m_sz;
* SParams(int sz) : m_sz(sz) {;}

};

int main(int argc, char *argv[])
{
* CFoo<2foo;

* const int sz(2);
* CFoo<szfoo2;

* int sz2(3);
* CFoo<sz2foo3;

* const SParams params(4);
* CFoo<params.m_s zfoo4;

}

When I compile this code I get the following output.

dwhs1@triton:~/test$ g++ -o template template.cpp
template.cpp:22 : error: sz2 cannot appear in a constant-expression
template.cpp:22 : error: template argument 1 is invalid
template.cpp:22 : error: invalid type in declaration before ; token
template.cpp: In function int main(int, char**):
template.cpp:25 : error: params cannot appear in a constant-expression
template.cpp:25 : error: `.' cannot appear in a constant-expression
template.cpp:25 : error: template argument 1 is invalid
template.cpp:25 : error: invalid type in declaration before ; token

I understand that in line 22 I am using a local variable that is not
const, but on line 25 there is a const member of a const structure.

What's going on here?

thanks

dan
constant exp and const object are different matters.the former is
usually refered to as literal or internally-linked value while the
second is called a read-only object.
A const object is that which is constructed at run-time and constant
since construction until destruction.
A constant exp is a value determined at compile-time.

regards,
FM.
Jun 27 '08 #3
On 10 mai, 06:57, terminator <farid.mehr...@ gmail.comwrote:
On May 9, 7:28 pm, Dan Smithers <dsmith...@talk talk.netwrote:
What constitutes a constant-expression? I know that it is
something that can be determined at compile time.
I am trying to use template code and keep getting compiler
errors "error: cannot appear in a constant-expression"
template <int s>
class CFoo
{
private:
int m_val[s];
};
struct SParams
{
const int m_sz;
SParams(int sz) : m_sz(sz) {;}
};
int main(int argc, char *argv[])
{
CFoo<2foo;
const int sz(2);
CFoo<szfoo2;
int sz2(3);
CFoo<sz2foo3;
const SParams params(4);
CFoo<params.m_s zfoo4;
}
When I compile this code I get the following output.
dwhs1@triton:~/test$ g++ -o template template.cpp
template.cpp:22 : error: sz2 cannot appear in a constant-expression
template.cpp:22 : error: template argument 1 is invalid
template.cpp:22 : error: invalid type in declaration before ; token
template.cpp: In function int main(int, char**):
template.cpp:25 : error: params cannot appear in a constant-expression
template.cpp:25 : error: `.' cannot appear in a constant-expression
template.cpp:25 : error: template argument 1 is invalid
template.cpp:25 : error: invalid type in declaration before ; token
I understand that in line 22 I am using a local variable
that is not const, but on line 25 there is a const member of
a const structure.
constant exp and const object are different matters.the former is
usually refered to as literal or internally-linked value while the
second is called a read-only object.
A const object is that which is constructed at run-time and constant
since construction until destruction.
A constant exp is a value determined at compile-time.
I might add that in C++, a const object of integral type *can*
be used in a constant expression, but only if its initializers
are visible and constant expressions.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4

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

Similar topics

7
5154
by: richbl | last post by:
Hello all, I have a question about unserializing a single array element from a serialized array. Can this be done, or must I first unserialize the array, and then access the element? For example, given: $data = serialize(array('4.50','0.00','0.00'));
0
1913
by: Andrea M. Segovia | last post by:
I just compiled (but did not install) perl 5.8.0 on an SGI Origin 300 server (IP35) running IRIX 6.5.20m. Make test reported one test error, which I narrowed down to .../lib/ExUtils/t/Constant.t using harness. I ran the test separately, and got the following detailed error report: .../lib/ExtUtils/t/Constant....ok 6/51Confused test output: test 6 answered after test 44
6
35683
by: fctk | last post by:
hello, i'm trying to compile this small program: int main(void) { unsigned long int max; max = 4000000000;
25
2600
by: tsaar2003 | last post by:
Hi Pythonians, To begin with I'd like to apologize that I am not very experienced Python programmer so please forgive me if the following text does not make any sense. I have been missing constants in Python language. There are some workarounds available, for example the const-module. To me, this looks quite cumbersome and very unintuitive. For the interpreter, in the efficiency-wise, I just cannot tell.
25
5741
by: galapogos | last post by:
Hi, I'm trying to compare an array of unsigned chars(basically just data without any context) with a constant, and I'm not sure how to do that. Say my array is array and I want to compare it with the constant 0x00010203040506070809 where array == 0x00, array == 0x01, etc. How do I do that. Obviously I can't use memcmp() with the 0x00010203040506070809 since the compiler will use that constant as an address and give me a segfault. Other...
22
5993
by: Laurent Deniau | last post by:
Is there any way to create a constant of type double _Complex without including <complex.h>? Why _Complex_I is a macro an not an implementation-defined constant? Thanks. a+, ld.
6
4026
by: Amit Bhatia | last post by:
Hi, I am not sure if this belongs to this group. Anyway, my question is as follows: I have a list (STL list) whose elements are pairs of integers (STL pairs, say objects of class T). When I create a new object of class T, I would like to check if this object already exists in the list: meaning one having same integers. This can be done in linear time in a list, and probably faster if I use STL Set instead of list. I am wondering however if...
33
5584
by: desktop | last post by:
In the C++ standard sec 23.1.2 table 69 it says that erase(q) where q is a pointer to an element can be done in amortized constant time. I guess that is not worst case since std::set is practically a red-black tree where insert/delete takes O(lg n) time. Or are there some other explanation for this complexity?
18
1941
by: sinbad | last post by:
hi, why does the following program gives an runtime error ,instead of compilation error. anyone please shed some light. thanks sinbad ------------------------------ int main()
2
4275
by: subramanian100in | last post by:
I am reading David Musser's "STL Tutorial and Reference Guide" Second Edition. In that book, on pages 68-69, definition has been given that "an iterator can be mutable or constant depending on whether the result of operator* is a reference or a constant reference." As per this definition, on page 71 in this book, it is mentioned that for 'set' and 'multiset', both the iterator and const_iterator types are constant bidirectional types -...
0
9961
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10778
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
10886
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
10439
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
9597
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7990
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4642
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
4245
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
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.