C2057: expected constant expression
-
23-12-2019 - |
Question
I can't compile this on VS2013, but it does compile on gcc 4.7.2. I understand VC++ is lagging behind GCC and CLang in terms of features, but what feature is it exactly and if not this is a bug, isn't?
template <int N>
struct factorial
{
static const long value;
static const long previous = factorial<N - 1>::value; //C2057: expected constant expression
};
template <int N>
const long factorial<N>::value = N*factorial<N - 1>::value;
template <>
struct factorial<0>
{
static const long value = 1;
};
Please don't tell me how I can get a workaround of this issue, cause this is not a production code neither I need one. For example I can define value in the class and the problem is solved, but let's assume I have to define it outside the class (VC2013 still doesn't support constexpr)
Solution
MSVC doesn't have a proper two-phase template compilation. GCC correctly sees factorial<N - 1>::value
as a dependent name and resolves in the second phase, but MSVC has to do it in the first phase.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow