Search code examples
c++visual-c++visual-studio-2013compiler-bug

(Known) compiler bug in VC12?


This program, when compiled with VC12 (in Visual Studio 2013 RTM)[1] leads to a crash (in all build configurations), when really it shouldn't:

#include <string>

void foo(std::string const& oops = {})
{
}

int main()
{
    foo();
}

I know of two silent bad codegen bugs that might be related:

Honestly I think these are different, though. Does anyone know

  1. whether there is an actively tracked bug on connect for this
  2. whether there is a workaround (or an explicit description of the situation that causes this bug, so we can look for it/avoid it in our code base)?

[1] Just create an empty project using the C++ Console Application 'wizard'. For simplicity, disable precompiled headers and leave all defaults: https://i.sstatic.net/rrrnV.png


Solution

  • An active issue was posted back in November. The sample code posted was:

    Compile and run following code in VS2013
    
    #include <string>
    
    void f(std::string s = {}) {
    }
    
    int main(int argc, char* argv[]) {
        f();
        return 0;
    }
    

    The bug has been acknowledged by Microsoft.

    There doesn't seem to be a work-around posted there. Edit Workarounds can easily be based on avoiding the list-initializer syntax:

    void f(std::string s = "");
    void f(std::string s = std::string());
    void f(std::string s = std::string {});
    

    Or just the old-fashioned (if you don't mind introducing overloads):

    void f(std::string s);
    void f() { f(std::string()); }