0

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?

Fido
  • 320
  • 1
  • 2
  • 15
  • Works for me: `Microsoft (R) C/C++ Optimizing Compiler Version 19.30.30709 for x64` – Dmytro Ovdiienko Feb 21 '22 at 11:20
  • 1
    Weird... after I updated to `Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31104 for x64`, the code does not compile with the `/std:c++20`. I get `error C2057: expected constant expression` error. It has to be a regression. Someone should file it to Microsoft :) Just for the record, `/std:c++17` works – Dmytro Ovdiienko Feb 21 '22 at 11:32
  • 1
    I've created an issue on the VS website: https://developercommunity.visualstudio.com/t/Cannot-use-template-function-type-in-con/1677526 – Fido Apr 17 '22 at 16:48

0 Answers0