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 8124441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:27:42+00:00 2026-06-06T06:27:42+00:00

OK, so first some things that might be relevant: I’m using the Clang 3.1

  • 0

OK, so first some things that might be relevant:

I’m using the Clang 3.1 compiler, in C++11 mode, with the standard library set to libc++.

I’m trying to familiarize myself with C++11, and in so doing I ran across behavior that seems odd. It may be a quirk of Clang or libc++ but I can’t speak C++ standardese and I have no access to other compilers with C++11 support so I can’t really check it, and I’ve searched the internet and Stack Overflow to the best of my ability without finding anything related…so here we go:

When using shared_ptr / unique_ptr to implement RAII for a simple resource, it seems that their behavior differs with respect to null pointers upon deletion. I realize that normally it’s not necessary to delete a null pointer, but I had expected the behavior to at least match between the two STL smart pointers.

For the specific case, consider the following code:

{
    auto Deleter = [](void *){cout << "It's later!" << endl;};
    shared_ptr<void> spDoSomethingLater(nullptr, Deleter);
    unique_ptr<void, void (*)(void *)> upDoSomethingLater(nullptr, Deleter);
    cout << "It's now!" << endl;
}

I would have expected one of the following outputs from this:

a) If both deleters are called even though the pointer is null:

"It's now!"
"It's later!"
"It's later!"

b) If neither deleter is called because the pointer is null:

"It's now!"

But I observe neither of these cases. Instead, I observe:

"It's now!"
"It's later!"

Which means one but not the other of the deleters is being called. Upon further investigation, I found that the deleter for shared_ptr is called regardless of whether it holds a null value, but unique_ptr’s deleter is only called if it does not hold a null value.

My questions:
Is this actually the correct behavior as specified by the standard? If so, why does the specified behavior differ between the two STL types in this manner? If not, is this a bug I should report to libc++?

  • 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-06T06:27:43+00:00Added an answer on June 6, 2026 at 6:27 am

    The observed behavior is in accordance with the standard.

    For unique_ptr, 20.7.1.2.2/2 (destructor effects) says

    Effects: If get() == nullptr there are no effects. Otherwise
    get_deleter()(get()).

    For shared_ptr, 20.7.2.2.2/1 says that the deleter should be called even if it wraps the null pointer:

    Effects:

    • If *this is empty or shares ownership with another
      shared_ptr instance (use_count() > 1), there are no side effects.
    • Otherwise, if *this owns an object p and a deleter d, d(p) is called.
    • Otherwise, *this owns a pointer p, and delete p is called.

    The important detail here is the expression “owns an object p“. 20.7.2.2/1 says that “a shared_ptr object is empty if it does not own a pointer”. 20.7.2.2.1/9 (the relevant constructor) says that it “constructs a shared_ptr object that owns the object p and the deleter d“.

    So as far as I can tell, that invocation technically makes the shared_ptr own the null pointer, which results in the deleter being called. Contrast this with the parameterless constructor which is said to leave the shared_ptr “empty“.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading some json output... just some integers. The first NSLog outputs things
I have some pages that have content that is relevant to both logged in
First, a brief description of the library that brought this up: I have a
In order to specify some things first: The user should be able to create
First, an example of something that works as expected: (all code was executed in
So basically I need to make feature that if you select something from first
First I want to clearify that I mean by reverse engineering something like decompiling
Normally, we get first value that way: $(#color option:first).val() But I need something like
I have some jQuery/JS below. The first thing to run is the alert box
Does anyone have some recommendations on how I can find databases for random things

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.