The items between [[ ]] in a function (variable...) declaration are attributes that provide the compiler with extra information about the item they are attached to. The compiler can use these in a range of ways. The "noreturn" attribute has been around since C++ 11.

[[ noreturn ]] attached to a function declaration informs the compiler that control will not return from the function (as in the case of an unconditional throw()). They go in the first declaration of the function. If f() is declared in a header file then this is the place to put it. If the function exists only in a single CPP file, i.e. it is private, with no other declaration then it goes there.

The second code snippet you posted is an example that can lead to undefined behaviour, i.e. returning from a function you explicitly declared would not return control.

Qt Code:
  1. // >>> function.h
  2. #ifndef FUNCTION_H
  3. #define FUNCTION_H
  4.  
  5. [[ noreturn ]] void f();
  6. void g();
  7.  
  8. #endif // FUNCTION_H
  9.  
  10. // >>> function.cpp
  11. #include <iostream>
  12. using namespace std;
  13.  
  14. #include "function.h"
  15.  
  16. void f() {
  17. cout << "No return" << endl;
  18. throw 1;
  19. }
  20.  
  21. void g() {
  22. cout << "No return" << endl;;
  23. throw 1;
  24. }
  25.  
  26. // >>> main.cpp
  27. #include <iostream>
  28. using namespace std;
  29.  
  30. #include "function.h"
  31.  
  32. [[ noreturn ]] void h() {
  33. cout << "No return" << endl;
  34. throw 3;
  35. }
  36.  
  37. int main(int argc, char **argv) {
  38. cout << "Before f()" << endl;
  39. f();
  40. // this should be unreachable code
  41. cout << "Before g()" << endl;
  42. g();
  43. cout << "Before h()" << endl;
  44. h();
  45. }
To copy to clipboard, switch view to plain text mode 

Functions f() and g() have identical function code but, because the compiler knows that f() will not return, it may generate different code for these function or calls to them. In my experiments with GNU C++ there is no code generated for the calls to g() and h() because they cannot be reached after a call to f().