0

Background: I'm trying to create perfect-forwarding factory methods for creating shared pointers of classes, where it's very clear when someone is calling one that might have a side-effect by taking in a non-const lvalue as a constructor parameter. See: SFINAE enable_if for variadic perfect forwarding template on reference/pointer const-ness So far so good.

However, now my problem is that when using make_shared to create classes that have private or protected constructors, even if they friend my 'builder' class template instance for themselves, the inner make_shared doesn't have access to the constructor.

I had hoped I could SFINAE on that too, by making a wrapper class which itself tries to use make_shared to create a member variable in the same fashion as my builder plans to, and then use that in an enable_if<is_constructible<...>> but that returns true (in gcc 4.8.2) even if trying to actually instantiate such a class doesn't compile:

template<typename R>
struct builder {

  class MakeSharedConstructTest
  {
  public:
    template<typename... Args>
    MakeSharedConstructTest(Args&&... args)
      : m_R(std::make_shared<R>(std::forward<Args>(args)...))
    {}
  private:
    std::shared_ptr<R> m_R;
  };

};

struct NonPublic
{
  friend class builder<NonPublic>;
private:
  NonPublic() {};
};

. . . 

// prints 1, ie yes, constructible
std::cout << std::is_constructible<builder<NonPublic>::MakeSharedConstConstructTest>::value << "\n"; 
// test.cpp:134:3: error: ‘NonPublic::NonPublic()’ is private
builder<NonPublic>::MakeSharedConstConstructTest q; 

What am I missing here?

Community
  • 1
  • 1
experquisite
  • 879
  • 5
  • 14
  • 1
    `is_constructible` tests the immediate context only. `MakeSharedConstructTest` has an accessible constructor with that signature, so `is_constructible` returns `true`. It doesn't care whether the constructor's body will compile. – T.C. May 06 '15 at 20:17
  • Thanks. Is there any way to do what I want that doesn't involve the passkey idiom (which requires changing the constructor signatures of the `NonPublic` classes) ? – experquisite May 06 '15 at 21:20

0 Answers0