Results 1 to 4 of 4

Thread: How to declare attribute 'noreturn'

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2016
    Qt products
    MacOS X

    Default How to declare attribute 'noreturn'

    I just opened a project in QT Creator 4.9 and in the application .cpp I get a warning that this function could include attribute 'noreturn', but I have not found how to declare this. I find examples of code such as:

    [[ noreturn ]] void f() {
    throw "error";
    // OK

    void q [[ noreturn ]] (int i) {
    // behavior is undefined if called with an argument <= 0
    if (i > 0) {
    throw "positive";

    but no clue as to where this goes--in the function .cpp? or the main.cpp? before the function? within the function?
    and I'm not at all familiar with the syntax [[ something ]].

    Or does it even matter whether the attribute is declared or not?


  2. #2
    Join Date
    Mar 2009
    Brisbane, Australia
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Unix/X11 Windows
    Wiki edits

    Default Re: How to declare attribute 'noreturn'

    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
    5. [[ noreturn ]] void f();
    6. void g();
    8. #endif // FUNCTION_H
    10. // >>> function.cpp
    11. #include <iostream>
    12. using namespace std;
    14. #include "function.h"
    16. void f() {
    17. cout << "No return" << endl;
    18. throw 1;
    19. }
    21. void g() {
    22. cout << "No return" << endl;;
    23. throw 1;
    24. }
    26. // >>> main.cpp
    27. #include <iostream>
    28. using namespace std;
    30. #include "function.h"
    32. [[ noreturn ]] void h() {
    33. cout << "No return" << endl;
    34. throw 3;
    35. }
    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().

  3. #3
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 709 Times in 699 Posts
    Qt products
    Windows Android

    Default Re: How to declare attribute 'noreturn'

    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().
    Wow, that seems like a recipe for descent into debugging hell and unsupportable code. What looks like perfectly acceptable code isn't, but that cannot be determined unless you are aware of the declaration of f().
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Nov 2016
    Ridgecrest California
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Windows Android

    Default Re: How to declare attribute 'noreturn'

    I realize this post is a little bit old, but I didn't see the solution that I had found so I thought I would respond. This may only be relevant if you're using MinGW. I found the following on the GCC online documentation web site at

    You may also specify attributes with `__' preceding and following each keyword. This allows you to use them in header files without being concerned about a possible macro of the same name. For example, you may use __noreturn__ instead of noreturn.
    Some programs define their own functions that never return. You can declare them noreturn to tell the compiler this fact. For example,

    Qt Code:
    1. void fatal () __attribute__ ((noreturn));
    2. void
    3. fatal (/* ... */)
    4. {
    5. /* ... */ /* Print error message. */ /* ... */
    6. exit (1);
    7. }
    To copy to clipboard, switch view to plain text mode 

    This solved my problem, hope it helps anybody else in this situation.

Similar Threads

  1. Attribute files
    By SirJonas in forum General Programming
    Replies: 1
    Last Post: 2nd November 2016, 20:24
  2. Replies: 1
    Last Post: 12th August 2009, 00:00
  3. QXmlStreamWriter and attribute namespaces
    By TomasC in forum Qt Programming
    Replies: 0
    Last Post: 11th December 2008, 06:11
  4. background attribute & url to file
    By Flakes in forum Qt Tools
    Replies: 6
    Last Post: 24th January 2008, 13:04
  5. Updating xml attribute
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th July 2007, 09:26

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.