Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:19:19+00:00 2026-06-04T18:19:19+00:00

I’m working on an application built with VC9 and I’ve hit upon a warning

  • 0

I’m working on an application built with VC9 and I’ve hit upon a warning I don’t fully understand: why is there an “unreachable code” warning on the closing brace of the constructor?

The minimal testcase to reproduce the issue is:

__declspec(noreturn) void foo() {
  // Do something, then terminate the program
}
struct A {
  A() {
    foo();
  } // d:\foo.cpp(7) : warning C4702: unreachable code
};
int main() {
  A a;
}

This must be compiled with /W4 to trigger the warning. Alternatively, you can compile with /we4702 to force an error on the detection of this warning.

d:\>cl /c /W4 foo.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

foo.cpp
d:\foo.cpp(7) : warning C4702: unreachable code

Can someone explain what, precisely, is unreachable here? My best theory is that it’s the destructor, but I’d like a definitive answer.

If I want to make this code warning-clean, how can I achieve that? The best I can come up with is convert this to a compile-time error.

struct A {
private:
  A(); // No, you can't construct this!
};
int main() {
  A a;
}

Edit: for clarification, terminating the program with a noreturn function doesn’t normally cause an unreachable code warning on the closing brace enclosing that function call.

__declspec(noreturn) void foo() {
  // Do something, then terminate the program
}
struct A {
  A() {
  }
  ~A() {
    foo();
  }
};
int main() {
  A a;
}

Results in:

d:\>cl /c /W4 foo3.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

foo3.cpp
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:19:21+00:00Added an answer on June 4, 2026 at 6:19 pm

    Gorpik is on the right track. I’ve created two similar test cases, compiled them, and disassembled them and I think I’ve come to understand the underlying reason: the constructor always generates a return statement implicitly and this return statement is unreachable due to the noreturn function.

    noreturn_constructor.cpp

    __declspec(noreturn) void foo() {
      // Do something, then terminate the program
    }
    struct A {
      A() {
        foo();
      }
      ~A() {
      }
    };
    int main() {
      A a;
    }
    

    noreturn_destructor.cpp

    __declspec(noreturn) void foo() {
      // Do something, then terminate the program
    }
    struct A {
      A() {
      }
      ~A() {
        foo();
      }
    };
    int main() {
      A a;
    }
    

    diff -u *.disasm

    --- noreturn_constructor.disasm 2012-05-30 11:15:02.000000000 -0400
    +++ noreturn_destructor.disasm  2012-05-30 11:15:08.000000000 -0400
    @@ -2,7 +2,7 @@
     Copyright (C) Microsoft Corporation.  All rights reserved.
    
    
    -Dump of file noreturn_constructor.obj
    +Dump of file noreturn_destructor.obj
    
     File Type: COFF OBJECT
    
    @@ -35,15 +35,15 @@
    
     ??0A@@QEAA@XZ (public: __cdecl A::A(void)):
       0000000000000000: 48 89 4C 24 08     mov         qword ptr [rsp+8],rcx
    -  0000000000000005: 48 83 EC 28        sub         rsp,28h
    -  0000000000000009: E8 00 00 00 00     call        ?foo@@YAXXZ
    -  000000000000000E: 48 8B 44 24 30     mov         rax,qword ptr [rsp+30h]
    -  0000000000000013: 48 83 C4 28        add         rsp,28h
    -  0000000000000017: C3                 ret
    +  0000000000000005: 48 8B 44 24 08     mov         rax,qword ptr [rsp+8]
    +  000000000000000A: C3                 ret
    
     ??1A@@QEAA@XZ (public: __cdecl A::~A(void)):
       0000000000000000: 48 89 4C 24 08     mov         qword ptr [rsp+8],rcx
    -  0000000000000005: C3                 ret
    +  0000000000000005: 48 83 EC 28        sub         rsp,28h
    +  0000000000000009: E8 00 00 00 00     call        ?foo@@YAXXZ
    +  000000000000000E: 48 83 C4 28        add         rsp,28h
    +  0000000000000012: C3                 ret
    
       Summary
    

    The unreachable code is this implicit return statement, which is generated in the constructor but not the destructor:

    -  000000000000000E: 48 8B 44 24 30     mov         rax,qword ptr [rsp+30h]
    +  0000000000000005: 48 8B 44 24 08     mov         rax,qword ptr [rsp+8]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Thanks in advance for your help. I have a need within an application to
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.