1 year ago

#205735

test-img

Fido

Cannot use template function type in constant context with MSVC 1930+ (Visual Studio 2022)

When I have a function template like this:

template<class T>
T func(T bar) {
    return bar;
}

I cannot use its instantiation in constant context with the latest MSVC compiler:

constexpr bool b = std::is_function_v<decltype(func<int>)>; // fails with error C2131: expression did not evaluate to a constant
static_assert(std::is_compound_v<decltype(func<int>)>); // fails with C2057: expected constant expression

You can test it yourself in compiler explorer.

It works with older MSVC versions and with GCC and Clang. Is it a compiler bug, or is it expected behavior?

c++

visual-studio-2022

function-templates

constant-expression

template-instantiation

0 Answers

Your Answer

Accepted video resources