//code1
class Test {
public:
constexpr Test(const char *p) : p_(p) {}
constexpr int foo() const
{
if(p_[0] != 'a')
return 1;
else
return 2;
}
const char *p_;
};
int arr[Test("bbb").foo()]; //this works
why the following code not work?
//code2
constexpr int foo() const
{
constexpr if (p_[0] != 'a') //add constexpr
return 1;
else
return 2;
}
Got an error:
error: expected unqualified-id before ‘if’
To my understanding, since "p_[0] != 'a'" can be evaluated at compile time(as shown in code1), so constexpr if (p_[0] != 'a') should be a valid statement which can be evaluate during compiling.