我正在製作一個小型系統,我希望能夠在整個系統中切換「詳細」文本輸出。使控制檯輸出成爲「詳細」的最快方法
我做了一個名爲globals.h:
namespace REBr{
extern bool console_verbose = false;
}
如果這是真的我希望我所有的類打印一條消息到控制檯時,他們正在建設,自毀,複製或做幾乎任何東西。
例如:
window(string title="",int width=1280,int height=720):
Width(width),Height(height),title(title)
{
if(console_verbose){
std::cout<<"Generating window #"<<this->instanceCounter;
std::cout<<"-";
}
this->window=SDL_CreateWindow(title.c_str(),0,0,width,height,SDL_WINDOW_OPENGL);
if(console_verbose)
std::cout<<"-";
if(this->window)
{
this->glcontext = SDL_GL_CreateContext(window);
if(console_verbose)
std::cout<<".";
if(this->glcontext==NULL)
{
std::cout<<"FATAL ERROR IN REBr::WINDOW::CONSTR_OPENGLCONTEXT: "<<SDL_GetError()<<std::endl;
}
}
else std::cout<<"FATAL ERROR IN REBr::WINDOW::CONSTR_WINDOW: "<<SDL_GetError()<<std::endl;
if(console_verbose)
std::cout<<">done!"<<endl;
}
現在你可以看到我有很多在構造IFS的。我真的不想這樣做,因爲這會減慢我的申請。我需要這樣做,儘可能快地移除「加載欄」(這有助於我確定程序停止運行的功能)。
什麼是最好的/最快的方法來完成這個?
萬萬在我的系統是命名空間REBr
將''console_verbose'標記爲'constexpr'。如果它們的表達式被評估爲「false」,那麼它顯然會導致編譯器優化所有的「if」。 – Alexey
此外,如果你想避免輸入'if(...)''每一次,只需定義一個宏#define VERB_STREAM if(console_verbose)std :: cout'並使用它像'VERB_STREAM <<「生成窗口#」<< this-> instanceCounter <<' - ';' – chtz
你應該也看到這個問題:https://stackoverflow.com/q/6692238/1632887 – seleciii44