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

  • Home
  • SEARCH
  • 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 842659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:59:54+00:00 2026-05-15T05:59:54+00:00

This code fails to compile using g++ 4.2.1 but works fine under vc++ v8.

  • 0

This code fails to compile using g++ 4.2.1 but works fine under vc++ v8.

#include <set>
typedef std::set<int *> IntPtrSet;
IntPtrSet iptrSet;

typedef std::set<shared_ptr<int>> IntPtrSet2;
IntPtrSet2 iptrSet2;

void AddIntegers(int& x)
{
    iptrSet.insert(&x);
    iptrSet2.insert(&x);
}

shared_ptr is similar to boost::shared_ptr or tr1::shared_ptr.

It emits the following errors,

No matching function for call to std::allocator<shared_ptr<int>>::construct(int**, const shared_ptr<int>&)
No matching function for call to std::allocator<shared_ptr<int> >::destroy(int **)

Did anyone encounter such error before? If yes, what is the workaround.

Here is the complete error message:

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:402: error: no matching function for call to 'std::allocator<shared_ptr<int> >::destroy(int**)'

/Users/mark/Templates/Function/main.cpp:188:   instantiated from here
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:380: error: no matching function for call to 'std::allocator<shared_ptr<int> >::construct(int**, const shared_ptr<int>&)'
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/new_allocator.h:106: note: candidates are: void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = shared_ptr<int>]

Complete Template Instantiation stack:

/Developer/usr/bin/gcc-4.2 -x c++ -arch x86_64 -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -isysroot 
/Developer/SDKs/MacOSX10.6.sdk -mfix-and-continue -fvisibility-inlines-hidden -mmacosx-version-min=10.6 -gdwarf-2 -iquote   /Users/mark/Templates/Function/build/Function.build/Debug/Function.build/Function-generated-files.hmap - I/Users/mark/Templates/Function/build/Function.build/Debug/Function.build/Function-own-target-headers.hmap - I/Users/mark/Templates/Function/build/Function.build/Debug/Function.build/Function-all- target-headers.hmap -iquote  /Users/mark/Templates/Function/build/Function.build/Debug/Function.build/Function-project- headers.hmap -F/Users/mark/Templates/Function/build/Debug - I/Users/mark/Templates/Function/build/Debug/include - I/Users/mark/Templates/Function/build/Function.build/Debug/Function.build/DerivedSources/x86 _64 - I/Users/mark/Templates/Function/build/Function.build/Debug/Function.build/DerivedSources -c  /Users/mark/Templates/Function/main.cpp -o  /Users/mark/Templates/Function/build/Function.build/Debug/Function.build/Objects- normal/x86_64/main.o

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h: In member function 'void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_destroy_node(std::_Rb_tree_node<_Val>*) [with _Key = shared_ptr<int>, _Val = shared_ptr<int>, _KeyOfValue = std::_Identity<shared_ptr<int> >, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]':

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:1327:   instantiated from 'void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_erase(std::_Rb_tree_node<_Val>*) [with _Key = shared_ptr<int>, _Val = shared_ptr<int>, _KeyOfValue = std::_Identity<shared_ptr<int> >, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:594:   instantiated from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::~_Rb_tree() [with _Key = shared_ptr<int>, _Val = shared_ptr<int>, _KeyOfValue = std::_Identity<shared_ptr<int> >, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_set.h:141:   instantiated from 'std::set<_Key, _Compare, _Alloc>::set() [with _Key = shared_ptr<int>, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]'

/Users/mark/Templates/Function/main.cpp:181: instantiated from here

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:402: error: no matching function for call to 'std::allocator<shared_ptr<int> >::destroy(int**)'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/new_allocator.h:110: note: candidates are: void __gnu_cxx::new_allocator<_Tp>::destroy(_Tp*) [with _Tp = shared_ptr<int>]

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h: In member function 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(const _Val&) [with _Key = shared_ptr<int>, _Val = shared_ptr<int>, _KeyOfValue = std::_Identity<shared_ptr<int> >, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]':

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:840:   instantiated from 'typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert(std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, const _Val&) [with _Key = shared_ptr<int>, _Val = shared_ptr<int>, _KeyOfValue = std::_Identity<shared_ptr<int> >, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:988:   instantiated from 'std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = shared_ptr<int>, _Val = shared_ptr<int>, _KeyOfValue = std::_Identity<shared_ptr<int> >, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_set.h:307:   instantiated from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = shared_ptr<int>, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]'

/Users/mark/Templates/Function/main.cpp:188:   instantiated from here

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:380: error: no matching function for call to 'std::allocator<shared_ptr<int> >::construct(int**, const shared_ptr<int>&)'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/new_allocator.h:106: note: candidates are: void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = shared_ptr<int>]

Here is the complete code:

#include <iostream>
template <class T>
class shared_ptr
{
    private:
    T* m_p;
public:
shared_ptr() throw() : m_p(NULL){}

shared_ptr( const shared_ptr<T>& p) throw()
{
      m_p = p;
}

shared_ptr( T* p) throw()
{
    m_p = p;
}

~shared_ptr() throw()
{
    m_p = NULL;
}

T* operator=(const shared_ptr<T>& p) throw()
{
    if (m_p != p.m_p)
    {
        m_p = p;
    }
    return m_p;
}

T* operator=(T* p) throw()
{
    if (m_p != p)
    {
        m_p = p;
    }
    return m_p;
}

operator T*() const throw()
{
    return m_p;
}

T& operator*() const throw()
{
    return *m_p;
}

T** operator&() throw()
{
    return &m_p;
}

bool operator!() const throw()
{
    return (m_p == NULL);
}

bool operator<(T* p) const throw()
{
    return m_p < p;
}

bool operator!=(int nNull) const throw()
{
    return !operator==(nNull);
}

bool operator==( int nNull) const throw()
{
    return m_p == NULL;
}

bool operator!=( T* p) const throw()
{
    return !operator==(p);
}

bool operator==( T* p) const throw()
{
    return m_p == p;
}

void CopyTo( T** pp) const throw()
{
    *pp = m_p;
}

void Release() throw()
{
    T* p = m_p;
    if (p)
    {
        m_p = NULL;
    }
}

void Attach( T* p) throw()
{
    m_p = p;
}

T* Detach() throw()
{
    T* p = m_p;
    m_p = NULL;
    return p;
}

};

#include <set>
typedef std::set<int *> IntPtrSet;
IntPtrSet iptrSet;

typedef std::set<shared_ptr<int> > IntPtrSet2;
IntPtrSet2 iptrSet2;

void AddIntegers(int& x)
{
iptrSet.insert(&x);

shared_ptr<int> intPtr(new int(3));

iptrSet2.insert(intPtr);
}


int main (int argc, char * const argv[]) {


char c;
std::cin >> c;
    return 0;
}

Also on the Dinkumware site the code compiled successfully. It fails to compile using g++ as mentioned earlier.FYI, I have pasted the result from the Dinkumware website (http://www.dinkumware.com/exam/default.aspx) below.

Your code has been compiled with the Microsoft Visual Studio 2005 C++ compiler using
the Dinkum C++ library from the Dinkum Compleat Libraries for VC++ package.

This is the compiler output using the code above in a file named
sourceFile.cpp:


sourceFile.cpp


size sourceFile.exe :
6144t + 3584 .rdata + 512d = 10240 (2800)

Code compiled successfully!
The executable generated was 11 KB.

Thanks a lot

Regards,
Mark

  • 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-05-15T05:59:55+00:00Added an answer on May 15, 2026 at 5:59 am

    Your shared_ptr implementation seems very unusual.

    Compared to boost/tr1, you have extra converting operators for T** and T*. Implicitly converting to the underlying pointer type (T*) is only going to confuse the compiler.

    After I commented out those methods, I had to fix the copy constructor to actually copy member-wise rather than abusing the converting operator. It still won’t work as a shared pointer because there’s no reference counting.

    Then I had to fix operator< to take const shared_ptr<T>& p again because the implicit conversions were causing this to compile, but confusing the compiler on the std::set later on. It got confused because it was trying to construct a shared_ptr but the operator& (I think) caused it to degrade to T** so the type didn’t match the type inside the container.

    With those changes I was able to get it to successfully compile with g++ 4.2.

    EDIT:
    Well, I was able to write a custom allocator that compiles but it doesn’t seem terribly clean and may not even work in all cases.

    template <class T>
    class sh_ptr_alloc : public std::allocator<T>
    {
    public:
            typedef size_t     size_type;
    
          sh_ptr_alloc() throw() { }
    
          template<typename Tp1>
            sh_ptr_alloc(const sh_ptr_alloc<Tp1>&) throw() { }
    
          template<typename Tp1>
            struct rebind
            { typedef sh_ptr_alloc<Tp1> other; };
    
    template <class Tp1>
          void
          deallocate(Tp1* p, size_type)
          { ::operator delete(p); }
    
    template <class Tp1>
          void 
          construct(Tp1** p, const shared_ptr<Tp1>& val) 
          { ::new(p) shared_ptr<int>(val); }
    
    template <class Tp1>
          void 
          destroy(Tp1** p) { }
    };
    

    The set is then:

    typedef std::set<shared_ptr<int>, std::less<shared_ptr<int> >, sh_ptr_alloc<shared_ptr<int> > > IntPtrSet2;

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

Sidebar

Related Questions

This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
I fail to understand why this code won't compile ExecutorService executor = new ScheduledThreadPoolExecutor(threads);
Why does this code fail to display the category name Apples using the current
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We
This code does not seem to compile, I just need to write something to
This code produces a FileNotFoundException, but ultimately runs without issue: void ReadXml() { XmlSerializer
This code in JS gives me a popup saying i think null is a
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
This code is executed by many way. When it's executed by the form button

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.