constant-expression

D

Dan Smithers

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

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@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
 
T

terminator

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;

};

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@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.
 
J

James Kanze

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;
};
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@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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top