std::unique_ptr of base class holding reference of derived class does not show warning in gcc compiler while naked pointer shows it. Why? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Why unique-ptr doesn't check base class to virtual destructible?Does delete work with pointers to base class?Why does an overridden function in the derived class hide other overloads of the base class?Why simple destructor does not delete the derived object if declared using base pointerCalling a Derived Class method from a Void Pointer cast to a Base Objectclang & gcc don't warn about non-virtual base destructors for polymorphism when using smart pointers?Why is initialization of derived class through a base class pointer different from that through a derived class pointer?Destructor when derived class contains a pointer to base class objectUsing base class rather than base pointer to work on derived classHow to prevent a derived class object under base class pointer calling a public nonvirtual function defined in base class but overridden in derived?How can I convert std::make_unique<derived>() to std::unique_ptr<base>

Retract an already submitted recommendation letter (written for an undergrad student)

Is there really no use for MD5 anymore?

Arriving in Atlanta after US Preclearance in Dublin. Will I go through TSA security in Atlanta to transfer to a connecting flight?

Why doesn't the standard consider a template constructor as a copy constructor?

Jaya, Venerated Firemage + Chandra's Pyrohelix = 4 damage among two targets?

Reattaching fallen shelf to wall?

What is the term for a person whose job is to place products on shelves in stores?

Is it acceptable to use working hours to read general interest books?

What was Apollo 13's "Little Jolt" after MECO?

What is /etc/mtab in Linux?

Was Dennis Ritchie being too modest in this quote about C and Pascal?

A Paper Record is What I Hamper

What is it called when you ride around on your front wheel?

How exactly does Hawking radiation decrease the mass of black holes?

Suing a Police Officer Instead of the Police Department

Does Feeblemind produce an ongoing magical effect that can be dispelled?

Contradiction proof for inequality of P and NP?

Why do games have consumables?

Why does Arg'[1. + I] return -0.5?

How to avoid introduction cliches

How do I check if a string is entirely made of the same substring?

All ASCII characters with a given bit count

Why must Chinese maps be obfuscated?

When do you need buffers/drivers on buses in a microprocessor design?



std::unique_ptr of base class holding reference of derived class does not show warning in gcc compiler while naked pointer shows it. Why?



Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Why unique-ptr doesn't check base class to virtual destructible?Does delete work with pointers to base class?Why does an overridden function in the derived class hide other overloads of the base class?Why simple destructor does not delete the derived object if declared using base pointerCalling a Derived Class method from a Void Pointer cast to a Base Objectclang & gcc don't warn about non-virtual base destructors for polymorphism when using smart pointers?Why is initialization of derived class through a base class pointer different from that through a derived class pointer?Destructor when derived class contains a pointer to base class objectUsing base class rather than base pointer to work on derived classHow to prevent a derived class object under base class pointer calling a public nonvirtual function defined in base class but overridden in derived?How can I convert std::make_unique<derived>() to std::unique_ptr<base>



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








7















I have a hierarchy of base class and derived class. Base class has one virtual function which is overridden by derived class.



class Base

public:
~Base();
virtual void other_functionality() = 0;
;

class Derived : public Base

public:
~Derived ();
void other_functionality() //some code;
;


Now if i do like this:



int main()

Base * P = new Derived ();
delete p;
return 0;



It gives error:
deleting object of polymorphic class type which has non-virtual destructor.



But with unique_ptr it passes without warning.



int main()

std::unique_ptr<Base> p;
p.reset(new Derived ());

return 0;



I know if I use virtual destructor. Warning with naked pointer will be solved. But question remains - why absence of virtual destructor shows problem with naked pointer and not with unique_ptr.










share|improve this question
























  • @AndriyTylychko unique_ptr exist in boost too and behavior of that and std::unique_ptr has lot of similarity. Isn't it.

    – gaurav bharadwaj
    1 hour ago











  • @ypnos do you not agree with above comment of mine?

    – gaurav bharadwaj
    1 hour ago






  • 1





    For the record, clang does complain: godbolt.org/z/qEp6Ts

    – Max Langhof
    1 hour ago






  • 1





    @P.W I don't think this is a duplicate. At least, answers to both questions are different. (Answer to the original question is "because the Stadnard does not require such a check". Answer to this question is "because gcc supresses warnings for system headers".)

    – Daniel Langr
    49 mins ago







  • 1





    @DanielLangr: The question seemed the same in essence. But the answers do not directly address why the compiler does not issue a diagnostic. So I will reopen.

    – P.W
    46 mins ago

















7















I have a hierarchy of base class and derived class. Base class has one virtual function which is overridden by derived class.



class Base

public:
~Base();
virtual void other_functionality() = 0;
;

class Derived : public Base

public:
~Derived ();
void other_functionality() //some code;
;


Now if i do like this:



int main()

Base * P = new Derived ();
delete p;
return 0;



It gives error:
deleting object of polymorphic class type which has non-virtual destructor.



But with unique_ptr it passes without warning.



int main()

std::unique_ptr<Base> p;
p.reset(new Derived ());

return 0;



I know if I use virtual destructor. Warning with naked pointer will be solved. But question remains - why absence of virtual destructor shows problem with naked pointer and not with unique_ptr.










share|improve this question
























  • @AndriyTylychko unique_ptr exist in boost too and behavior of that and std::unique_ptr has lot of similarity. Isn't it.

    – gaurav bharadwaj
    1 hour ago











  • @ypnos do you not agree with above comment of mine?

    – gaurav bharadwaj
    1 hour ago






  • 1





    For the record, clang does complain: godbolt.org/z/qEp6Ts

    – Max Langhof
    1 hour ago






  • 1





    @P.W I don't think this is a duplicate. At least, answers to both questions are different. (Answer to the original question is "because the Stadnard does not require such a check". Answer to this question is "because gcc supresses warnings for system headers".)

    – Daniel Langr
    49 mins ago







  • 1





    @DanielLangr: The question seemed the same in essence. But the answers do not directly address why the compiler does not issue a diagnostic. So I will reopen.

    – P.W
    46 mins ago













7












7








7








I have a hierarchy of base class and derived class. Base class has one virtual function which is overridden by derived class.



class Base

public:
~Base();
virtual void other_functionality() = 0;
;

class Derived : public Base

public:
~Derived ();
void other_functionality() //some code;
;


Now if i do like this:



int main()

Base * P = new Derived ();
delete p;
return 0;



It gives error:
deleting object of polymorphic class type which has non-virtual destructor.



But with unique_ptr it passes without warning.



int main()

std::unique_ptr<Base> p;
p.reset(new Derived ());

return 0;



I know if I use virtual destructor. Warning with naked pointer will be solved. But question remains - why absence of virtual destructor shows problem with naked pointer and not with unique_ptr.










share|improve this question
















I have a hierarchy of base class and derived class. Base class has one virtual function which is overridden by derived class.



class Base

public:
~Base();
virtual void other_functionality() = 0;
;

class Derived : public Base

public:
~Derived ();
void other_functionality() //some code;
;


Now if i do like this:



int main()

Base * P = new Derived ();
delete p;
return 0;



It gives error:
deleting object of polymorphic class type which has non-virtual destructor.



But with unique_ptr it passes without warning.



int main()

std::unique_ptr<Base> p;
p.reset(new Derived ());

return 0;



I know if I use virtual destructor. Warning with naked pointer will be solved. But question remains - why absence of virtual destructor shows problem with naked pointer and not with unique_ptr.







c++ c++14 gcc-warning






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









ypnos

37.2k1377113




37.2k1377113










asked 1 hour ago









gaurav bharadwajgaurav bharadwaj

507617




507617












  • @AndriyTylychko unique_ptr exist in boost too and behavior of that and std::unique_ptr has lot of similarity. Isn't it.

    – gaurav bharadwaj
    1 hour ago











  • @ypnos do you not agree with above comment of mine?

    – gaurav bharadwaj
    1 hour ago






  • 1





    For the record, clang does complain: godbolt.org/z/qEp6Ts

    – Max Langhof
    1 hour ago






  • 1





    @P.W I don't think this is a duplicate. At least, answers to both questions are different. (Answer to the original question is "because the Stadnard does not require such a check". Answer to this question is "because gcc supresses warnings for system headers".)

    – Daniel Langr
    49 mins ago







  • 1





    @DanielLangr: The question seemed the same in essence. But the answers do not directly address why the compiler does not issue a diagnostic. So I will reopen.

    – P.W
    46 mins ago

















  • @AndriyTylychko unique_ptr exist in boost too and behavior of that and std::unique_ptr has lot of similarity. Isn't it.

    – gaurav bharadwaj
    1 hour ago











  • @ypnos do you not agree with above comment of mine?

    – gaurav bharadwaj
    1 hour ago






  • 1





    For the record, clang does complain: godbolt.org/z/qEp6Ts

    – Max Langhof
    1 hour ago






  • 1





    @P.W I don't think this is a duplicate. At least, answers to both questions are different. (Answer to the original question is "because the Stadnard does not require such a check". Answer to this question is "because gcc supresses warnings for system headers".)

    – Daniel Langr
    49 mins ago







  • 1





    @DanielLangr: The question seemed the same in essence. But the answers do not directly address why the compiler does not issue a diagnostic. So I will reopen.

    – P.W
    46 mins ago
















@AndriyTylychko unique_ptr exist in boost too and behavior of that and std::unique_ptr has lot of similarity. Isn't it.

– gaurav bharadwaj
1 hour ago





@AndriyTylychko unique_ptr exist in boost too and behavior of that and std::unique_ptr has lot of similarity. Isn't it.

– gaurav bharadwaj
1 hour ago













@ypnos do you not agree with above comment of mine?

– gaurav bharadwaj
1 hour ago





@ypnos do you not agree with above comment of mine?

– gaurav bharadwaj
1 hour ago




1




1





For the record, clang does complain: godbolt.org/z/qEp6Ts

– Max Langhof
1 hour ago





For the record, clang does complain: godbolt.org/z/qEp6Ts

– Max Langhof
1 hour ago




1




1





@P.W I don't think this is a duplicate. At least, answers to both questions are different. (Answer to the original question is "because the Stadnard does not require such a check". Answer to this question is "because gcc supresses warnings for system headers".)

– Daniel Langr
49 mins ago






@P.W I don't think this is a duplicate. At least, answers to both questions are different. (Answer to the original question is "because the Stadnard does not require such a check". Answer to this question is "because gcc supresses warnings for system headers".)

– Daniel Langr
49 mins ago





1




1





@DanielLangr: The question seemed the same in essence. But the answers do not directly address why the compiler does not issue a diagnostic. So I will reopen.

– P.W
46 mins ago





@DanielLangr: The question seemed the same in essence. But the answers do not directly address why the compiler does not issue a diagnostic. So I will reopen.

– P.W
46 mins ago












2 Answers
2






active

oldest

votes


















12














Well, first of all, deleting a derived object through a base pointer when the base class does not have a virtual destructor is undefined behavior. Compilers are not required to diagnose undefined behavior…



That being said, the reason why this warning does not appear when using std::unique_ptr is most likely due to the fact that GCC does not report warnings that would appear in system headers.






share|improve this answer























  • That's a good find from GCC manual.

    – P.W
    35 mins ago


















6














I cannot find a link, but I did see a discussion of this online, in GCC bug database.



The warning is issued on the actual delete expression. In the case of unique_ptr, the delete is called inside a system header file.



According to the discussion in that bug report, implementing C++ system libraries requires all sorts of compromises that result in various warnings. Therefore, the warnings are restricted inside system headers. That is the reason you won't see the warning you expect.



Update: and here it is, straight from the horse's mouth:



https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html




The header files declaring interfaces to the operating system and runtime libraries often cannot be written in strictly conforming C. Therefore, GCC gives code found in system headers special treatment. All warnings, other than those generated by ‘#warning’ (see Diagnostics), are suppressed while GCC is processing a system header.







share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55848866%2fstdunique-ptr-of-base-class-holding-reference-of-derived-class-does-not-show-w%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    12














    Well, first of all, deleting a derived object through a base pointer when the base class does not have a virtual destructor is undefined behavior. Compilers are not required to diagnose undefined behavior…



    That being said, the reason why this warning does not appear when using std::unique_ptr is most likely due to the fact that GCC does not report warnings that would appear in system headers.






    share|improve this answer























    • That's a good find from GCC manual.

      – P.W
      35 mins ago















    12














    Well, first of all, deleting a derived object through a base pointer when the base class does not have a virtual destructor is undefined behavior. Compilers are not required to diagnose undefined behavior…



    That being said, the reason why this warning does not appear when using std::unique_ptr is most likely due to the fact that GCC does not report warnings that would appear in system headers.






    share|improve this answer























    • That's a good find from GCC manual.

      – P.W
      35 mins ago













    12












    12








    12







    Well, first of all, deleting a derived object through a base pointer when the base class does not have a virtual destructor is undefined behavior. Compilers are not required to diagnose undefined behavior…



    That being said, the reason why this warning does not appear when using std::unique_ptr is most likely due to the fact that GCC does not report warnings that would appear in system headers.






    share|improve this answer













    Well, first of all, deleting a derived object through a base pointer when the base class does not have a virtual destructor is undefined behavior. Compilers are not required to diagnose undefined behavior…



    That being said, the reason why this warning does not appear when using std::unique_ptr is most likely due to the fact that GCC does not report warnings that would appear in system headers.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 59 mins ago









    Michael KenzelMichael Kenzel

    8,64811525




    8,64811525












    • That's a good find from GCC manual.

      – P.W
      35 mins ago

















    • That's a good find from GCC manual.

      – P.W
      35 mins ago
















    That's a good find from GCC manual.

    – P.W
    35 mins ago





    That's a good find from GCC manual.

    – P.W
    35 mins ago













    6














    I cannot find a link, but I did see a discussion of this online, in GCC bug database.



    The warning is issued on the actual delete expression. In the case of unique_ptr, the delete is called inside a system header file.



    According to the discussion in that bug report, implementing C++ system libraries requires all sorts of compromises that result in various warnings. Therefore, the warnings are restricted inside system headers. That is the reason you won't see the warning you expect.



    Update: and here it is, straight from the horse's mouth:



    https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html




    The header files declaring interfaces to the operating system and runtime libraries often cannot be written in strictly conforming C. Therefore, GCC gives code found in system headers special treatment. All warnings, other than those generated by ‘#warning’ (see Diagnostics), are suppressed while GCC is processing a system header.







    share|improve this answer



























      6














      I cannot find a link, but I did see a discussion of this online, in GCC bug database.



      The warning is issued on the actual delete expression. In the case of unique_ptr, the delete is called inside a system header file.



      According to the discussion in that bug report, implementing C++ system libraries requires all sorts of compromises that result in various warnings. Therefore, the warnings are restricted inside system headers. That is the reason you won't see the warning you expect.



      Update: and here it is, straight from the horse's mouth:



      https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html




      The header files declaring interfaces to the operating system and runtime libraries often cannot be written in strictly conforming C. Therefore, GCC gives code found in system headers special treatment. All warnings, other than those generated by ‘#warning’ (see Diagnostics), are suppressed while GCC is processing a system header.







      share|improve this answer

























        6












        6








        6







        I cannot find a link, but I did see a discussion of this online, in GCC bug database.



        The warning is issued on the actual delete expression. In the case of unique_ptr, the delete is called inside a system header file.



        According to the discussion in that bug report, implementing C++ system libraries requires all sorts of compromises that result in various warnings. Therefore, the warnings are restricted inside system headers. That is the reason you won't see the warning you expect.



        Update: and here it is, straight from the horse's mouth:



        https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html




        The header files declaring interfaces to the operating system and runtime libraries often cannot be written in strictly conforming C. Therefore, GCC gives code found in system headers special treatment. All warnings, other than those generated by ‘#warning’ (see Diagnostics), are suppressed while GCC is processing a system header.







        share|improve this answer













        I cannot find a link, but I did see a discussion of this online, in GCC bug database.



        The warning is issued on the actual delete expression. In the case of unique_ptr, the delete is called inside a system header file.



        According to the discussion in that bug report, implementing C++ system libraries requires all sorts of compromises that result in various warnings. Therefore, the warnings are restricted inside system headers. That is the reason you won't see the warning you expect.



        Update: and here it is, straight from the horse's mouth:



        https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html




        The header files declaring interfaces to the operating system and runtime libraries often cannot be written in strictly conforming C. Therefore, GCC gives code found in system headers special treatment. All warnings, other than those generated by ‘#warning’ (see Diagnostics), are suppressed while GCC is processing a system header.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 55 mins ago









        ArkadiyArkadiy

        18.2k559102




        18.2k559102



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55848866%2fstdunique-ptr-of-base-class-holding-reference-of-derived-class-does-not-show-w%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Oświęcim Innehåll Historia | Källor | Externa länkar | Navigeringsmeny50°2′18″N 19°13′17″Ö / 50.03833°N 19.22139°Ö / 50.03833; 19.2213950°2′18″N 19°13′17″Ö / 50.03833°N 19.22139°Ö / 50.03833; 19.221393089658Nordisk familjebok, AuschwitzInsidan tro och existensJewish Community i OświęcimAuschwitz Jewish Center: MuseumAuschwitz Jewish Center

            Valle di Casies Indice Geografia fisica | Origini del nome | Storia | Società | Amministrazione | Sport | Note | Bibliografia | Voci correlate | Altri progetti | Collegamenti esterni | Menu di navigazione46°46′N 12°11′E / 46.766667°N 12.183333°E46.766667; 12.183333 (Valle di Casies)46°46′N 12°11′E / 46.766667°N 12.183333°E46.766667; 12.183333 (Valle di Casies)Sito istituzionaleAstat Censimento della popolazione 2011 - Determinazione della consistenza dei tre gruppi linguistici della Provincia Autonoma di Bolzano-Alto Adige - giugno 2012Numeri e fattiValle di CasiesDato IstatTabella dei gradi/giorno dei Comuni italiani raggruppati per Regione e Provincia26 agosto 1993, n. 412Heraldry of the World: GsiesStatistiche I.StatValCasies.comWikimedia CommonsWikimedia CommonsValle di CasiesSito ufficialeValle di CasiesMM14870458910042978-6

            Typsetting diagram chases (with TikZ?) Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)How to define the default vertical distance between nodes?Draw edge on arcNumerical conditional within tikz keys?TikZ: Drawing an arc from an intersection to an intersectionDrawing rectilinear curves in Tikz, aka an Etch-a-Sketch drawingLine up nested tikz enviroments or how to get rid of themHow to place nodes in an absolute coordinate system in tikzCommutative diagram with curve connecting between nodesTikz with standalone: pinning tikz coordinates to page cmDrawing a Decision Diagram with Tikz and layout manager