Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: constant-ex...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 45827 of 47017
Post > Topic >>

Re: constant-expression

by James Kanze <james.kanze@[EMAIL PROTECTED] > May 10, 2008 at 08:06 AM

On 10 mai, 06:57, terminator <farid.mehr...@[EMAIL PROTECTED]
> wrote:
> On May 9, 7:28 pm, Dan Smithers <dsmith...@[EMAIL PROTECTED]
> 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<2> foo;

> >   const int sz(2);
> >   CFoo<sz> foo2;

> >   int sz2(3);
> >   CFoo<sz2> foo3;

> >   const SParams params(4);
> >   CFoo<params.m_sz> foo4;
> > }

> > When I compile this code I get the following output.

> > dwhs1@[EMAIL PROTECTED]
 g++ -o template template.cpp
> > template.cpp:22: error: =91sz2=92 cannot appear in a
constant-expression=

> > template.cpp:22: error: template argument 1 is invalid
> > template.cpp:22: error: invalid type in declaration before =91;=92
token=

> > template.cpp: In function =91int main(int, char**)=92:
> > template.cpp:25: error: =91params=92 cannot appear in a
constant-express=
ion
> > 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 =91;=92
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:james.kanze@[EMAIL PROTECTED]
 en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
 




 4 Posts in Topic:
constant-expression
Dan Smithers <dsmither  2008-05-09 16:28:05 
Re: constant-expression
"Victor Bazarov"  2008-05-09 11:43:36 
Re: constant-expression
terminator <farid.mehr  2008-05-09 21:57:41 
Re: constant-expression
James Kanze <james.kan  2008-05-10 08:06:56 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 16:09:28 CDT 2008.