I want to refine advanced variational arguments except for a certain type

I have the following

#include <iostream>
#include <memory>

template<typename _type>
class handle
{
  using ptr = std::shared_ptr<_type>;
  using pptr = std::shared_ptr<ptr>;
public:
  handle(handle<_type> const & other) :
    mData(make_pptr(*(other.mData)))
  {}


  handle(_type && data) :
    mData(make_pptr(std::move(data)))
  {}
private:
  pptr mData;

  template<typename ..._args>
  constexpr auto make_ptr(_args && ...args)
  {
    return std::make_shared<_type>(std::forward<_args>(args)...);
  }

  constexpr auto make_pptr(ptr const & pointer)
  {
    return std::make_shared<ptr>(pointer);
  }

  template<typename ..._args>
  constexpr auto make_pptr(_args && ...args)
  {
    return std::make_shared<ptr>(make_ptr(std::forward<_args>(args)...));
  }
};

int main()
{
  handle<int> h = 5;
  handle<int> h2(h);
}

Compiled with g++-4.9 --std=c++14 -O0 -o main main.cppcode

handle<int> h2(h);

not compiled. The problem functions are all overloads.

make_pptr

As I understand it, the template function will always be selected, since the compiler is trying to find the function call the most , and perfect forwarding creates exactly that.

I found the following two pages that seem to handle this issue with type std::enable_ifand type std::is_same.

https://akrzemi1.wordpress.com/2013/10/10/too-perfect-forwarding/

http://www.codesynthesis.com/~boris/blog/2012/05/30/perfect-forwarding-and-overload-resolution/

, , , factory ?

?

+4
2

Jarod ,

handle(handle<_type> const & other) :
  mData(make_pptr(*(other.mData)))
{}

make_pptr shared_ptr<_type>&, make_pptr , , shared_ptr<_type> const&. const&, , make_pptr, const lvalue.

constexpr auto make_pptr(ptr & pointer)
{
  return std::make_shared<ptr>(pointer);
}

- , , shared_ptr<_type>.

, shared_ptr<T>

namespace detail
{
    template<typename... _args>
    using zeroth_type = typename std::tuple_element<0, std::tuple<_args...>>::type;

    template<typename T, bool eval_args, typename... _args>
    struct is_shared_ptr
    : std::false_type
    {};

    template<typename T, typename... _args>
    struct is_shared_ptr<T, true, _args...>
    : std::is_same<std::decay_t<zeroth_type<_args...>>,
                   std::shared_ptr<T>
                  >
    {};
}

make_pptr

template<typename ..._args,
         typename = std::enable_if_t<
                        not detail::is_shared_ptr<_type, sizeof...(_args), _args...>::value
                    >
        >
constexpr auto make_pptr(_args && ...args)
{
  return std::make_shared<ptr>(make_ptr(std::forward<_args>(args)...));
}

make_ptr, , , , _type nullptr.

constexpr auto make_ptr()
{
  return std::make_shared<_type>();
  // no nullptr arg above, shared_ptr default ctor will initialize _type* to nullptr
}

+2

handle<int> h2(h);

handle(handle<_type> const & other) : mData(make_pptr(*(other.mData)))
{}

*other.mData std::shared_ptr<_type>&,

template<typename ..._args>
constexpr auto make_pptr(_args && ...args)

.

const

handle(handle<_type> const & other) :
    mData(make_pptr(static_cast<const ptr&>(*(other.mData))))
{}

.

.

0
source

All Articles