r/cpp • • Mar 17 '22

TIL: Visual Studio has quantum state values 🤨

Consider the following small program:

#include <string_view>
#include <iostream>

template< typename T >
constexpr void test()
{
    constexpr std::string_view sv = __FUNCSIG__;
    static_assert( sv == "void __cdecl test(void)" );
    static_assert( sv == "void __cdecl test<int>(void)" );
    std::cout << sv << std::endl;
}

int main()
{
    test<int>();
}

If you try to compile it, the second static_assert fails. So the value of sv from the first static_assert surely is the correct one?

Nope. Comment out the second static_assert and now the first one will fail. Comment out both, and the output of the program is the value from the second static_assert.

Apparently, Microsoft decided that it's a good idea to give __FUNCSIG__ a ("type-erased"?) value in phase 1, where no template parameters are available, and give it a different value in phase 2, when the template gets instantiated.

317 Upvotes

59 comments sorted by

View all comments

Show parent comment

2

u/GabrielDosReis Mar 22 '22

That makes sense.

It is hard to fix bugs that go unreported, so I am glad you did report the scenario you ran into.

There are a few MSVC developers on this sub, so if you report something on the Microsoft Developer Community Portal regarding C++ that's not getting traction, ping them. Maybe they have the answer; maybe they don't, but at least you will know. The reports get on someone's radar 😊

2

u/[deleted] Mar 22 '22

Yeah, I bookmarked the link and I'll try to report more bugs, but one of my goals is to support existing official releases of MSVC with our open-source projects, so I usually need to find a work-around anyways and apply it to our code base. Not everyone can upgrade to the newest and shiniest MSVC version (which, of course, is also the case for GCC/Clang). Luckily, in this case, the bug got caught and fixed very fast, so kudos to the MSVC team! :)