简体   繁体   中英

C2057: expected constant expression

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM