Permission overload function using weak_ptr as argument

I have:

class A : public std::enable_shared_from_this<A>
{...};

class B : public A
{...}; 

void doCoolStuff(std::weak_ptr<A> obj)
{...}

void doCoolStuff(std::weak_ptr<B> obj)
{
 ...
 doCoolStuff(std::static_pointer_cast<A>(obj.lock())); (1)
}

      

And then in function B:

void B::doReallyCoolStuff()
{
 doCoolStuff(std::static_pointer_cast<B>(shared_from_this())); (2)
}

      

So the problems are:

  • Compiler error: error C2440: 'static_cast' : cannot convert from 'B *const ' to 'A *'

  • Compiler error: error C2668: ambiguous call to overloaded function

I don't understand how to solve any of them because:

  • I think it has something to do with shared_from_this because it is a const pointer . But I don't know how to handle this situation without const_cast.
  • I don't know if functions can be overloaded with various types of weak pointers.

Build environment: MSVS 2013 Express

Please, help. Thanks you

+3


source to share


2 answers


As for problem (2), you can of course overload like this. But the problem is that you are calling the function with a type std::shared_ptr<B>

. This requires an implicit conversion std::weak_ptr

, and it can be converted in std::weak_ptr<A>

, and in std::weak_ptr<B>

. Both are implemented with an implicit conversion constructor internally std::weak_ptr

, which means neither is better than the other. Hence the ambiguity.

To solve this problem, you can specify the type explicitly:

void B::doReallyCoolStuff()
{
    doCoolStuff(std::weak_ptr<B>(std::static_pointer_cast<B>(shared_from_this())));
}

      



Live example

Alternatively, you can provide overloads doCoolStuff

with std::shared_ptr

.

As the live example above shows, I was unable to reproduce the issue (1).

+6


source


You might like to review the arguments for your functions.

Functions should only accept a smart pointer if they need to store the pointer somewhere after returning. Otherwise, functions must accept references or simple pointers.



According to Herb Sutter in Back to Basics! Functions that accept smart pointers (by value or by reference) are anti-templating if those functions should not save, modify, or drop a reference:

  • Don't use native raw *, new, or delete anymore, except rarely in the implementation details of low-level data structures.

  • Use non-owning raw * and &, especially for parameters.

  • Don't copy / assign refcounted smart pointers, including pass-by-value, or in loops unless you really want the semantics to be expressed: changing the lifetime of an object.

+2


source







All Articles