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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:17:11+00:00 2026-06-16T11:17:11+00:00

I’m attempting to use std::unique_ptr in order to store the integer handle to some

  • 0

I’m attempting to use std::unique_ptr in order to store the integer handle to some opaque objects. For this purpose, I have defined a custom deleter type which does typedef int pointer in order to override the raw pointer type to int instead of int*. This process is described in the last section of this site: http://asawicki.info/news_1494_unique_ptr_in_visual_c_2010.html

Here is some sample code to better illustrate what I’m attempting to do:

#include <memory>
#include <iostream>

static void close(int p)
{
    std::cout << p << " has been deleted!" << std::endl;
}

struct handle_deleter
{
    typedef int pointer;
    void operator()(pointer p) { close(p); }
};

typedef std::unique_ptr< int, handle_deleter> unique_handle;

int main(int argc, char *argv[])
{
    unique_handle handle(1);

    return 0;
}

When I compile this code using GCC 4.7.2, I get the following error:

In file included from /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/memory:86:0,
                 from unique_ptr_test.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/unique_ptr.h: In instantiation of ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = int; _Dp = handle_deleter]’:
unique_ptr_test.cpp:19:23:   required from here
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/unique_ptr.h:172:2: error: invalid operands of types ‘int’ and ‘std::nullptr_t’ to binary ‘operator!=’

The code of the ~unique_ptr procedure reads as follows:

// Destructor.
~unique_ptr() noexcept
{
    auto& __ptr = std::get<0>(_M_t);
    if (__ptr != nullptr)
      get_deleter()(__ptr);
    __ptr = pointer();
}

According to me, the check against nullptr does not make sense since the raw pointer type is int (and not int* due to overriding it in HandleDeleter). Strangely enough, this code compiles with no error under GCC 4.6.1. Upon execution, the sample displays “1 has been deleted!” as expected.

I was wondering if there is any detail I’m overlooking or if it really is a bug within GCC’s STL implementation of unique_ptr.

Thanks,

PMJ

  • 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-16T11:17:16+00:00Added an answer on June 16, 2026 at 11:17 am

    As I said in my comment, if the deleter type you pass has a nested pointer typedef, it must fulfill the NullablePointer requirements:

    20.7.1.2 [unique.ptr.single] p3

    If the type remove_reference<D>::type::pointer exists, then unique_ptr<T, D>::pointer shall be a synonym for remove_reference<D>::type::pointer. Otherwise unique_ptr<T, D>::pointer shall be a synonym for T*. The type unique_ptr<T, D>::pointer shall satisfy the requirements of NullablePointer (17.6.3.3).

    And §17.6.3.3 then lists all the requirements a type has to fulfill to be a NullablePointer. Certain semantics are listed in a table, where:

    u denotes an identifier, t denotes a non-const lvalue of type P, a and b denote values of type (possibly const) P, and np denotes a value of type (possibly const) std::nullptr_t.

    Expression    Return type                         Operational semantics
    P u(np);                                          post: u == nullptr
    P u = np;
    P(np)                                             post: P(np) == nullptr
    t = np        P&                                  post: t == nullptr
    a != b        contextually convertible to bool    !(a == b)
    a == np       contextually convertible to bool    a == P()
    np == a
    a != np       contextually convertible to bool    !(a == np)
    np != a
    

    Now, the simplest solution is to wrap your int in a type that provides these semantics:

    #include <cstddef> // std::nullptr_t
    
    struct handle{
      handle() : value(0){}
      handle(std::nullptr_t) : value(0){}
      /*explicit*/ handle(int v) : value(v){} // make it explicit if you need it
      // special members can be generated
    
      handle& operator=(std::nullptr_t){ value = 0; return *this; }
    
      // contextual conversion to bool
      explicit operator bool() const{ return value != 0; }
    
      int value;
    };
    
    bool operator==(handle lhs, handle rhs){ return lhs.value == rhs.value; }
    bool operator!=(handle lhs, handle rhs){ return lhs.value != rhs.value; }
    // comparision against 'nullptr' is handled by the above operators
    // since 'nullptr' can be implicitly converted to 'handle'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.