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 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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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'));
|
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...
|
by: fctk |
last post by:
hello,
i'm trying to compile this small program:
int main(void) {
unsigned long int max;
max = 4000000000;
|
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...
|
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...
| |
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.
|
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...
|
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?
|
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()
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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
|
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...
| |