VC2019: Compiler Bug, std::pair Bug or Obscure Feature

117 views Asked by At

The code below crashes the application or prints gibberish. If the initialization of the base class is replaced with str_int{ string{V}, 0} then it works fine. Seems to work fine with some online compilers.

#include <iostream>
#include <utility>

using namespace std;

using str_int = pair<string, int>;

template< const char* V > 
struct C : public str_int
{
  C() : str_int{ V, 0} {}
};

constexpr const char str[] = "abc";

int main()
{
  // works fine
  str_int si{str, 0};
  cout << si.first;

  // crashes the application or prints gibberish
  C<str> c;
  cout << c.first;
}
1

There are 1 answers

1
Alex Guteniev On

This was a bug in the compiler, I've reported it based on the reduced version of the repro in the question couple of years ago. (I've reduced it to avoid STL usage, clean compiler repros are better for such cases.)

It is fixed in the current Visual Studio version.

See https://developercommunity.visualstudio.com/t/bad-code-generation-on-passing-template-/1534999