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

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_szfoo4;
}

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 4741
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_szfoo4;
}

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...@talktalk.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_szfoo4;

}

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...@talktalk.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_szfoo4;
}
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 objektorientierter Datenverarbeitung
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
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...
0
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...
6
by: fctk | last post by:
hello, i'm trying to compile this small program: int main(void) { unsigned long int max; max = 4000000000;
25
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...
25
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...
22
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
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...
33
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...
18
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.