So, playing around with constexpr, MSVC (Visual Studio 2012) gave me an error while trying to qualify my function with the constexpr
keyword using this simple program (includes omitted):
constexpr int factorial(int n)
{
return n <= 1 ? 1 : (n * factorial(n-1));
}
int main(void)
{
const int fact_three = factorial(3);
std::cout << fact_three << std::endl;
return 0;
}
constexpr
was underlined red with the following message:
Error : this declaration has no storage class or type specifier
and trying to compile the program gave the following output:
1>main.cpp(5): error C2144: syntax error : 'int' should be preceded by ';'
1>main.cpp(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
It really puzzles me as it is the very example that Cppreference uses to illustrate the use of constexpr
. At first I used a simple function that returned a literal, i.e. constexpr int func(){return 5;}
, but which yielded the same error. I interpreted the first message as "it should be a member function of a struct or class", but the example from Cppreference shows that it's not necessary apparently.
So, what am I obviously missing here ?
question from:
https://stackoverflow.com/questions/18513618/why-does-msvc11-rejects-constexpr-qualification-of-a-function 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…