_ Declspec (noreturn) Usage

Source: Internet
Author: User

_ Declspec (noreturn) Usage

Floating white clouds 2008.11.26 first look at the annotation of noreturn in msdn:

A function is _ Declspec (noreturn)As agreed, it means to tell the compiler that this function will not be returned, and the result is to let the compiler know that the call convention is _ Declspec (noreturn)The code after the function is not reachable.
If the compiler finds that a function has a code branch with no return value, the compiler reports a c4715 warning or c2202 error message. If this code branch cannot be reached because the function does not return, you can use the Convention. _ Declspec (noreturn)To avoid the above warnings or errors.
Note:

Convention on a function to be returned_ Declspec (noreturn)Will lead to undefined behavior.

Example:

In the following example, the main function is notElseBranch return, so the agreed FunctionFatalIs_ Declspec (noreturn)To avoid compilation or warning.

// noreturn2.cpp__declspec(noreturn) extern void fatal () {}int main() { if(1) return 1; else if(0) return 0; else fatal();}
----------------------------------------------------------- Another purpose is to use _ Declspec (noreturn)To define the throw function. Therefore, when an exception is thrown, the subsequent statement is not executed. Example:
  1. // Exception
  2. Class exception {
  3. Public:
  4. Virtual ~ Exception ()
  5. {
  6. }
  7. };
  8. // Exception
  9. // Logicalerror
  10. Class logicalerror:
  11. Public exception {
  12. Cstringw m_message;
  13. Public:
  14. Logicalerror (lpcwstr MSG ):
  15. M_message (MSG)
  16. {
  17. }
  18. Virtual ~ Logicalerror ()
  19. {
  20. }
  21. Lpcwstr getmessage () const
  22. {
  23. Return m_message;
  24. }
  25. Static void decl_noreturn throw (lpcwstr MSG)
  26. {
  27. Throw logicalerror (MSG );
  28. }
  29. };

In use, the code will not be executed after the throw statement is called.

For example: lpcwstr getstring (File * FP) {If (FP = NULL) {logicalerror: Throw (L "getstring (File * FP): file handle FP is null! ");}......}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.