473,548 Members | 2,598 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 4755
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 orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 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
5134
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
1890
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...
6
35655
by: fctk | last post by:
hello, i'm trying to compile this small program: int main(void) { unsigned long int max; max = 4000000000;
25
2547
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...
25
5687
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...
22
5950
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
3988
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...
33
5528
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
1916
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
4254
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...
0
7512
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...
0
7438
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7951
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...
1
7466
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...
0
7803
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...
0
6036
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3495
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...
1
1926
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
0
751
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...

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.