ここに小さなプログラムがあります:
#include <iostream>
#include <string>
#include <cstdlib>
void Exit_With_Error(std::string const& error_message);
int main(){
Exit_With_Error("Error X occurred.");
return 0;
}
void Exit_With_Error(std::string const& error_message){
std::cerr << error_message << std::endl;
exit(EXIT_FAILURE);
return;
}
ご覧のとおり、関数Exit_With_Errorは実際には戻りません。それをよりよく説明するために、属性を追加すると思いました。Documentation hereは、次のように見えるはずだと私に信じさせます。
#include <iostream>
#include <string>
#include <cstdlib>
[[noreturn]] void Exit_With_Error(std::string const& error_message);
int main(){
Exit_With_Error("Error X occurred.");
return 0;
}
[[noreturn]] void Exit_With_Error(std::string const& error_message){
std::cerr << error_message << std::endl;
exit(EXIT_FAILURE);
return;
}
ただし、コンパイルされません。
main.cpp:6:1: error: expected unqualified-id before ‘[’ token
main.cpp: In function ‘int main()’:
main.cpp:9:37: error: ‘Exit_With_Error’ was not declared in this scope
main.cpp: At global scope:
main.cpp:13:1: error: expected unqualified-id before ‘[’ token
私はこれを機能させました!
#include <iostream>
#include <string>
#include <cstdlib>
__attribute__((noreturn)) void Exit_With_Error(std::string const& error_message);
int main(){
Exit_With_Error("Error X occurred.");
return 0;
}
__attribute__((noreturn)) void Exit_With_Error(std::string const& error_message){
std::cerr << error_message << std::endl;
exit(EXIT_FAILURE);
return;
}
私の質問: [[attribute]] 構文を機能させるにはどうすればよいですか? gcc で c++11 フラグを使用してコンパイルしています。たとえば、
g++ -std=c++11 -o main main.cpp
それでも機能しません。コンパイラのバージョンは 4.7.2 です。DOES がうまく機能する方法はありますか、それともより単純な構文を目指して努力する必要がありますか?