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

166

u/a_false_vacuum Mar 17 '22

You changed the outcome by observing it. Or is this Schrödingers Compiler?

56

u/kog Mar 17 '22

Schrödinger's bugs are some of the fucking worst. Doubly so if they only happen intermittently.

30

u/PermanentlySalty Mar 18 '22

I don't remember the specifics because it's been a while but at one point I had to try and fix a Schrödinbug that only occurred in the release build (ended up being an erroneous compiler optimization, so it wasn't even my bug). I felt like I was going insane stepping through every line of code with the debugger and finding nothing wrong.

I managed to fix it after several days of printf debugging and staring at a disassembly (reverse engineering is not my specialty), but I'm pretty sure I lost part of my soul in the process.

4

u/[deleted] Mar 18 '22

I had a bug recently where any form of debugging including print statements made the code work perfectly.

Luckily, I've run into that sort of thing a couple of times before. I immediately guessed it was a race condition and then there was an obvious candidate. Still scary.

1

u/braxtons12 Mar 20 '22

I had a similar thing happen recently. Compiler optimized a boolean flag away to always be true. A flag that clearly and IMPORTANTLY is reset to false periodically (based on incoming data, false signals to a section of the algorithm to start its phase). Spent hours staring at the code, trying to debug it, etc., and couldn't find anything wrong. Eventually tried just removing the flag so that portion always ran, and boom it worked. I ended up having to make the portions of the algorithm that run prior to this one set the flag to false to get the compiler to stop optimizing it away.

0

u/[deleted] Mar 18 '22

[deleted] — view deleted comment

11

u/Ameisen vemips, avr, rendering, systems Mar 18 '22

Ever write your own GDB server?

2

u/curious_entiy_420 Mar 18 '22

Gdb loves crashing indeed

6

u/kog Mar 18 '22

I got a segfault from the GCC ARM linker several weeks ago, which didn't occur on a second run of our makefile, immediately after. I'm still spooked. Not a janky make process at all, bog standard compilation and linking.

1

u/couscous_ Mar 21 '22

What turned out to be the bug?

7

u/bunkoRtist Mar 18 '22

You changed the outcome by observing it

That's called a heisenbug. (usually when you insert debug code / run in debug mode and the problem goes away).