Results 1 to 4 of 4

Thread: How to declare attribute 'noreturn'

  1. #1
    Join Date
    Jun 2016
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    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?

    Thanks,
    Bob

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,554
    Thanks
    13
    Thanked 1,581 Times in 1,510 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    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
    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().

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,195
    Thanks
    237
    Thanked 668 Times in 658 Posts
    Qt products
    Qt5
    Platforms
    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
    Location
    Ridgecrest California
    Posts
    33
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    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 https://gcc.gnu.org/onlinedocs/gcc-4...ttributes.html.

    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, 21:24
  2. Replies: 1
    Last Post: 12th August 2009, 01:00
  3. QXmlStreamWriter and attribute namespaces
    By TomasC in forum Qt Programming
    Replies: 0
    Last Post: 11th December 2008, 07:11
  4. background attribute & url to file
    By Flakes in forum Qt Tools
    Replies: 6
    Last Post: 24th January 2008, 14:04
  5. Updating xml attribute
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th July 2007, 10:26

Tags for this Thread

Bookmarks

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.