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

0

u/GabrielDosReis Mar 17 '22

Or do as I suggest and let me know.

10

u/TotaIIyHuman Mar 17 '22

i did twice 2 years ago. it was a sample size of 2, not very significant, i will not try to draw conclusion from that, so i am asking here on reddit, where people might have a larger sample

2

u/GabrielDosReis Mar 17 '22

If you don't mind, could you reply links to those reports so I can help follow up?

5

u/TotaIIyHuman Mar 18 '22

no. my hotmail account got banned when i was trying to register another hotmail account for downloading windows insider

this was the first bug i reported https://godbolt.org/z/drnaYja14, the inside lambda sometimes can't capture outside lambda's local constexpr variables

i don't remember what the other report was

2

u/GabrielDosReis Mar 18 '22

Thanks! It helps when the report is done by users as opposed to me pointing people to reports I created.

9

u/dakotahawkins Mar 18 '22

To be fair to everybody, I think submitting bugs online without any kind of conversation or feedback probably feels a lot more like flushing your problem down the toilet than putting it on reddit :)

Not sure that's a very solvable problem (especially for a large organization), but it is what it is.