Search code examples
c++templatesc++11stlshared-ptr

can I explicitly specialize std:shared_ptr::operator ->


my first time asking a question, so fingers crossed.

I would like to provide a specialization of shared_ptr::operator-> The following wont compile on gcc 4.8.3

template<> 
widget* std::shared_ptr<widget>::operator->() const 
{ 
    return nullptr; 
}

I get the following compile error error: no member function ‘operator->’ declared in ‘std::shared_ptr’

I've tried a few variations on how to specialize this and I keep getting the same error. e.g wrapping it up in a namespace std scope, also tried trailing reutrn types, making it noexcept etc etc


Solution

  • No, you cannot specialize member functions from types within std. Doing so makes your program ill-formed (and I believe no diagnostic is required). Even if it "works" it is illegal.

    You can specialize the entire type for a user-supplied type, but that basically involves rewriting std::shared_ptr from scratch.