我正在使用Allegro创建一个简单的游戏。当我尝试验证我的指针显示不为空,我得到一个编译器错误,告诉我将std:unique_ptr隐式强制转换为bool时出错
错误C2664:“无效的validate(BOOL,的std :: string)”:不能转换 参数1从'的std ::的unique_ptr < ALLEGRO_DISPLAY,主要:: < lambda_996846ce92067e506da99cad36e610cf >>”到 '布尔'
这里是我的代码
#include <iostream>
#include <memory>
#include <string>
#include <allegro5\allegro.h>
using namespace std;
const int WIDTH = 512;
const int HEIGHT = 512;
void validate(bool ptr, string errorMessage) {
if (!ptr) {
cerr << errorMessage << endl;
exit(-1);
}
}
int main() {
auto deleter = [](ALLEGRO_DISPLAY* d) { al_destroy_display(d); };
unique_ptr<ALLEGRO_DISPLAY, decltype(deleter)> display;
validate(al_init(), "Failed to initialize Allegro");
display = unique_ptr<ALLEGRO_DISPLAY, decltype(deleter)>(al_create_display(WIDTH, HEIGHT), deleter);
validate(display, "Failed to create display");
return 0;
}
如果我通过验证“!display”而不是“display”它的工作原理。我意识到我可以用display.get()调用validate,但是我想知道为什么当我传递一个智能指针时它不工作。
我发现了这个错误报告。我正在使用Visual Studio 2013. https://connect.microsoft.com/VisualStudio/feedbackdetail/view/775810/c-11-std-unique-ptr-cast-to-bool-fails-with-deleter-lambda