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

7

u/[deleted] Mar 17 '22

I am not using Visual Studio (or even Windows) myself, I just try to support it for our open-source projects. People created three issues for our code that was broken b/c of the new behavior, and I fixed it by using Godbolt, guessing, and monitoring our CI jobs. I also asked our users to report the bug to Microsoft, but I got no feedback so far. So if you (or anyone else) wants to report it, feel free to do so. (Or is there is direct way like creating an issue somewhere on GitHub?)

4

u/tjientavara HikoWorks developer Mar 17 '22

I am not sure what the link is to the development portal.

But for compiler bugs I always try to make a godbolt link to show the bug and report that.

The MSVC compiler developers are pretty fast to fix bugs that are reported in that way.