The*_*ign 6 c++ c++17 visual-c++-2019
下面的代码使应用程序崩溃或打印出乱码。如果基类的初始化被替换为str_int{ string{V}, 0}那么它就可以正常工作。似乎与一些在线编译器一起工作得很好。
#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;
}
Run Code Online (Sandbox Code Playgroud)
这是编译器中的一个错误,我根据几年前问题中的重现的简化版本报告了它。(我已经减少了它以避免使用 STL,干净的编译器重现对于这种情况更好。)
它已在当前 Visual Studio 版本中修复。
请参阅https://developercommunity.visualstudio.com/t/bad-code- Generation-on-passing-template-/1534999