0

Lets say I have the following:

int i = 1;
String str("abc");

Would str be consider a constant expression?

From lots of C++ books, it seems a constant expression must be evaluated to an integral type.

yapkm01
  • 3,590
  • 7
  • 37
  • 62

1 Answers1

2

Would str be consider a constant expression?

No, it won't. In C++11 there is a new keyword constexpr introduced that helps generalize the notion of constant expressions. If String constructor from "abc" is trivial enough then it could be declared constexpr; however such constructor probably has to allocate memory so it wouldn't qualify.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • In the above case, variable i might not be a constant expression too since it could be changed, am i right? A constant expression must be something that is definite and wont change - hence the compiler can determine its value. Only thing i am puzzle is does it needs to be an integral type? How about a constant string like const string("abc");? – yapkm01 Nov 15 '11 at 00:28
  • @yapkm01: Yes, in C++03 constant expressions must be of integral type. – K-ballo Nov 15 '11 at 00:31