Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top