class-static constexpr function not considered constexpt in vs c++ 17 when reference from template member

Michal Krombholz 5 Reputation points
2023-03-19T19:40:17.2233333+00:00

The following code does not compile with vs c++ 17 (vs2022) but does compile with c++ 15 (vs2017) and gcc10/11.

class X
{
public:
    static constexpr bool constexprFoo()
    {
        return true;
    }

    template<class T>
    T foo()
    {
        if constexpr (constexprFoo()) {}
        return {};
    }
}

1>TestConstexpr.cpp

1>C:\home\c\TestConstexpr\TestConstexpr.cpp(12,35): error C2131: expression did not evaluate to a constant

1>C:\home\c\TestConstexpr\TestConstexpr.cpp(12,35): message : failure was caused by call of undefined function or one not declared 'constexpr'

1>C:\home\c\TestConstexpr\TestConstexpr.cpp(12,35): message : see usage of 'X::constexprFoo'

1>C:\home\c\TestConstexpr\TestConstexpr.cpp(10,7): message : This diagnostic occurred in the compiler generated function 'T X::foo(void)'

1>Done building project "TestConstexpr.vcxproj" -- FAILED.

However, moving constexprFoo outside of the class or accessing it from a non-template member works fine.

Developer technologies C++
Developer technologies Visual Studio Other
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.