test.cpp:在函数`int main()''中: test.cpp:4:警告:ISO C ++禁止将`sizeof''应用于函数 类型 --- 除了那个警告之外,汇编也会通过。 不应该它失败了?我缺少什么? 谢谢, - #1 - pg。 21,现代C ++设计,Andrei Alexandrescu。in general, checks for incomplete types are better done within sizeofbecause it''s runtime overheads are minimum. try to generate theassembly code (using, g++ -S) with and without the "sizeof" and you''llsee that the sizeof version is better.however, i have a related, but different problem. i tried the code asprinted in the book[#1] and couldn''t get it to work.---/* begin code */template<boolstruct CompileTimeChecker{ CompileTimeChecker(...); };template<struct CompileTimeChecker<false{ };#define STATIC_CHECK(expr, msg) { class ERROR_##msg {};(void)sizeof(CompileTimeChecker<(expr) != 0>((ERROR_##msg()))); }int main(){ STATIC_CHECK(1>2,mustFail);}/* end code */---here''s what g++ gives me:---g++ -Wall -pedantic test.cpptest.cpp: In function `int main()'':test.cpp:4: warning: ISO C++ forbids applying `sizeof'' to a functiontype---apart from that warning, the compilation goes through.shouldn''t it fail? what am i missing?thanks,--#1 - pg. 21, Modern C++ Design, Andrei Alexandrescu. 我不知道。 但我认为最好在1> 2中添加()I don''t know.but i think it would be better to add () in 1>2 这篇关于为什么这段代码必须添加sizeof()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 13:26