Jump to content
  • Advertisement
Sign in to follow this  
Azune

Explicit instantiation of placement new template

This topic is 4666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is a very simplified version of the smart pointer I'm working on. It will involve simulating intrusiveness by doing some extra stuff during allocation via an overloaded new operator (hacked and unportable I hear you cry :p).
#include <cstdlib>

template<class T> class sptr {
    public:
        class new_flags {};
        
        T* p;
};

template<class T>
void* operator new(size_t size, const typename sptr<T>::new_flags& flags)
{
    // Do stuff ...
}

template<class T>
void* foo(size_t size, const typename sptr<T>::new_flags& flags)
{
    // Do stuff ...
}

int main()
{
    // This won't compile:
    // int* p = new(sptr<int>::new_flags()) int(123);

    // Neither will this:
    // void* q = foo(0, sptr<int>::new_flags());
    
    // However this will:
    void* r = foo<int>(0, sptr<int>::new_flags());
}
I get this error when compiling:
Quote:
ptr.cpp: In function `int main()': ptr.cpp:25: error: no matching function for call to `operator new(unsigned int, sptr<int>::new_flags)' <internal>:0: note: candidates are: void* operator new(unsigned int)
It doesn't seem to be able to see my overloaded new. When I couldn't solve this I tried changing the function from 'operator new' to 'foo'. Again it didn't compile with implicit instantiation, but did compile with explicit instantiation. So basically I need to know how I can explicity instantiate my overloaded new operator or if it's even possible. I tried the following:
void* p = operator new<int>(0, sptr<int>::new_flags());
which works but obviously will only allocate the memory, not construct the object. Thanks in advance for any help you are able to offer. PS: I am aware that there are good existing implementations of smart pointers but I'm still playing with my own version because programming is a hobby I enjoy doing. Hopefully that should ward off unhelpful shouts of 'use boost::shared_ptr' ;).

Share this post


Link to post
Share on other sites
Advertisement
As far as I can tell, you've got two separate problems: one, you are trying to perform type deduction on a nested type of an template class, and two, you're syntax looks like a cast of a function pointer declaration to the compiler.

The first one, I believe, is probably a limitation of compiler technology. I could be wrong about that one. The second one, is a problem with your usage of the placment new syntax and the fact that the compiler, if it can parse something as a function declaration, it will..

To address both points you may consider doing something like:

template <typename T>
class sptr_new_flags {};

int main()
{
sptr_new_flags<int> obj;

int* p = new (obj) int(123);


If you rather not have the named temporary, you can get around the parsing problem with adding an additional set of parenthesis around the placement new argument.

int* p = new ( (sptr_new_flags<int>()) ) int(123);

This seems kind of hackish to me.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!