Eya*_*nik 6 c++ unique-ptr c1001
在发布模式下编译时出现以下错误.
1>d:\users\eyal\projects\code\yalla\core\src\runbox\win32\window.cpp : fatal error C1001: An internal error has occurred in the compiler.
1> (compiler file 'f:\dd\vctools\compiler\utc\src\p2\main.c', line 249)
1> To work around this problem, try simplifying or changing the program near the locations listed above.
1> Please choose the Technical Support command on the Visual C++
1> Help menu, or open the Technical Support help file for more information
1> link!RaiseException()+0x48
1> link!CxxThrowException()+0x65
1> link!std::_Xout_of_range()+0x1f
1> link!InvokeCompilerPass()+0x1b4e2
1> link!InvokeCompilerPass()+0x22efe
1> link!InvokeCompilerPass()+0x2332e
1> link!InvokeCompilerPass()+0x232f9
1> link!InvokeCompilerPass()+0x233cb
1> link!InvokeCompilerPass()+0x22b04
1> link!InvokeCompilerPass()+0x22d86
1> link!DllGetC2Telemetry()+0x115837
1>
1> 1>
1>LINK : fatal error LNK1257: code generation failed
Run Code Online (Sandbox Code Playgroud)
我正在使用VS2015 Update 2 RC.
我不确定但也许这是优化器中的错误?
导致它的代码如下:
在window.h
class Window {
public:
Window();
~Window();
void show();
void hide();
private:
class NativeControl;
std::unique_ptr<NativeControl> _window;
};
Run Code Online (Sandbox Code Playgroud)
window.cpp
class Window::NativeControl : public NativeWindow {
public:
NativeControl() : NativeWindow() { }
};
Window::Window()
: _window(std::make_unique<Window::NativeControl>()) {
}
Window::~Window() {
}
void Window::show()
{
_window->show(WindowShowMode::Show);
}
void Window::hide()
{
_window->show(WindowShowMode::Hide);
}
Run Code Online (Sandbox Code Playgroud)
NativeWindow是任何操作系统的本机窗口.
这是使用GCC 5.1编译的工作代码: https ://ideone.com/4YvjRK
只是为了做一个笔记.
如果我将删除继承并用这样的东西替换它.
class Window::NativeControl {
public:
void show(WindowShowMode showMode)
{
}
};
Run Code Online (Sandbox Code Playgroud)
它会工作正常!
以下是使用GCC 5.1编译的相同代码,没有继承: https ://ideone.com/Mu0A42
似乎导致此行为的是从NativeWindow派生NativeControl.
重现它的步骤如下:
1> C:\ Program Files(x86)\ Microsoft Visual Studio 14.0\VC\include\memory(1194):错误C2338:无法删除不完整类型1> 1> 1> C:\ Program Files(x86)\ Microsoft Visual Studio 14.0\VC\include\memory(1195):警告C4150:删除指向不完整类型'Yalla :: Window :: NativeControl'的指针; 没有析构函数称为1>
d:\ Users\Eyal\Projects\Code\Yalla\core\src\runbox\include\window.h(13):注意:请参阅'Yalla :: Window :: NativeControl'1>
窗口的声明.cpp 1> 1>构建失败.
有趣的是,重建似乎解决了这个问题!
我想要实现的是基本上将NativeWindow的实际实现放在不同的文件中,主要是为了简单而不是可重用性.
我想这可能会混淆unique_ptr模板,而不是在继承中这样做,我也可以通过组合来做到这一点,并通过getter公开NativeWindow的实例,它可能会工作,但问题是是否有更好的方法来做到这一点?
经过很长一段时间我没有碰到它,我正在重新学习C++,所以如果我正在做的一些事情没有意义,请告诉我它!
更新:
C++标准说:
unique_ptr的模板参数T可以是不完整类型.
我在Herb Sutter的博客中发现了一篇关于它的帖子.
小智 2
类似的错误与
(编译器文件'f:\dd\vctools\compiler\utc\src\p2\main.c',第255行)
已修复Properties->Linker->Optimization->Link Time Code Generation
从更改/LTCG:incremental为/LTCG
Studio 2015 更新 3