我正在使用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