Results 1 to 3 of 3

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,531
    Thanks
    13
    Thanked 1,576 Times in 1,505 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
    3,988
    Thanks
    232
    Thanked 628 Times in 618 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 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.

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

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.