c++functionc++11attributesnoreturn

Do C++11 attributes work on g++ 4.7.2?


Here is a small program:

#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;
}

As you can see, the function Exit_With_Error, never actually returns. I thought I'd tack on an attribute to better illustrate that. Documentation here, leads me to believe that it should look like this:

#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;
}

However, it does not compile:

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

I got this to work though!

#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;
}

My question: How do I get the [[attribute]] syntax to work? I am compiling with the c++11 flag on gcc. So for example,

g++ -std=c++11 -o main main.cpp

yet it's not working. I have version 4.7.2 of the compiler. Is the way that DOES work all right, or should I strive for that simpler syntax?


Solution

  • No. Generalized Attributes were not implemented in GCC until version 4.8 This can be seen here:
    http://gcc.gnu.org/gcc-4.7/cxx0x_status.html
    http://gcc.gnu.org/gcc-4.8/cxx0x_status.html