3

I'm writing a sort of "asynchronous factory" where time-consuming constructions of concrete objects are deferred to std::async tasks. Each AsyncFactory will store a smart pointer to the object.

[This is not the most correct application of Factory Pattern, but it is for the sake of a MWE].

#include <future>
#include <memory>
#include <type_traits>
#include <cassert>

/**
 * @param I is the interface type
 * @param Ptr is the memory handler. Default = unique; optional = shared
 */
template <class I, template<class> class Ptr = std::unique_ptr>
class AsyncFactory
{
    /**
     * @param C - the concrete type for the interface I
     * @param Ts - the variadic params
     */
    template <class C, typename... Ts>
    void _future_reload(Ts&&... params)
    {
        if (std::is_same<Ptr, std::unique_ptr>())                       // line21
        {
            ptr = std::make_unique<C>(std::forward<Ts>(params)...);
        }
        else
        {
            if (std::is_same<Ptr, std::shared_ptr>())                   // line27
            {
                ptr = std::make_shared<C>(std::forward<Ts>(params)...);
            }
            else
            {
                static_assert(0, "unacceptable type for smart pointer");// line33
            }
        }
    }

public: 

    Ptr<I> ptr;

    AsyncFactory() :
        ptr(nullptr)
    {}

    /**
     * @param C - the concrete type. Default: the interface type
     * @param Ts - the variadic params
     */
    template <class C = I, typename... Ts>
    void reload(Ts&&... params)
    {
        std::future<void> fReload =
                        std::async(std::launch::async,
                        &AsyncFactory::_future_reload<C, Ts...>, this,
                        std::forward<Ts>(params)...);
    }
};


class BaseVirtual
{
    virtual void foo() = 0;
};

class DerivedConcrete :
    public BaseVirtual
{
    void foo() override {;}
};


int main()
{
    AsyncFactory<BaseVirtual, std::shared_ptr> fac;

    fac.reload<DerivedConcrete>();
}

Problems arise with the smart pointer. I have to call different makers for unique/shared pointers. But g++ -std=c++14 stops with

f.cpp: In member function ‘void AsyncFactory<I, Ptr>::_future_reload(Ts&& ...)’:
f.cpp:21:44: error: type/value mismatch at argument 1 in template parameter list for ‘template<class, class> struct std::is_same’
   if (std::is_same<Ptr, std::unique_ptr>())
                                        ^
f.cpp:21:44: note:   expected a type, got ‘Ptr’
f.cpp:21:44: error: type/value mismatch at argument 2 in template parameter list for ‘template<class, class> struct std::is_same’
f.cpp:21:44: note:   expected a type, got ‘unique_ptr’
f.cpp:27:45: error: type/value mismatch at argument 1 in template parameter list for ‘template<class, class> struct std::is_same’
    if (std::is_same<Ptr, std::shared_ptr>())
                                         ^
f.cpp:27:45: note:   expected a type, got ‘Ptr’
f.cpp:27:45: error: type/value mismatch at argument 2 in template parameter list for ‘template<class, class> struct std::is_same’
f.cpp:27:45: note:   expected a type, got ‘shared_ptr’
f.cpp:33:9: error: static assertion failed: unacceptable type for smart pointer
     static_assert(0, "unacceptable type for smart pointer");
Patrizio Bertoni
  • 2,582
  • 31
  • 43

2 Answers2

7

std::unique_ptr is not a type. std::unique_ptr<int> is a type. You need to pass explicit template parameters to use it inside is_same.

Also, you probably don't want to use if in this way, as both branches need to be valid regardless of the result of is_same. In C++17, you would use if constexpr(...) to solve this issue - in C++14, you can use a more traditional overload-based approach or tag-dispatch based approach. E.g.

auto impl(std::true_type  /* is shared ptr */) { /* ... */ }
auto impl(std::false_type /* is unique ptr */) { /* ... */ }

Usage:

ptr = impl(std::is_same<Ptr, std::shared_ptr<T>>{}, /* ... */);
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • The problem is `Ptr = std::shared_ptr` as of `main` call; further, I just need to know which `std::make_` to call. Because of the concrete-interface layer, I cannot assume to have `std::is_same, std::shared_ptr>{}` working, generally speaking – Patrizio Bertoni May 02 '18 at 13:16
2

You cannot compare the template template parameters like that, you can only compare concrete types with std::is_same. Further, you cannot even have the same template signature of AsyncFactory for both std::shared_ptr and std::unique_ptr anymore: https://godbolt.org/g/tNiYB1

A solution to your problem is introducing another layer of abstraction:

struct UseUnique
{
    template<class I>
    using Ptr = std::unique_ptr<I>;

    template<class C, typename... Ts>
    static auto build(Ts&&... params)
    {
        return std::make_unique<C>(std::forward<Ts>(params)...);
    }
};

struct UseShared
{
    template<class I>
    using Ptr = std::shared_ptr<I>;

    template<class C, typename... Ts>
    static auto build(Ts&&... params)
    {
        return std::make_shared<C>(std::forward<Ts>(params)...);
    }
};

These structs contain the information you need to define your member and to build concrete types (i.e. which pointer type and which make_X function to use). Then you can do:

template <class I, class UseWhat = UseShared>
class AsyncFactory
{
    template <class C, typename... Ts>
    void _future_reload(Ts&&... params)
    {
        ptr = UseWhat::template build<C>(std::forward<Ts>(params)...);
    }

public:
    using Ptr = typename UseWhat::template Ptr<I>;
    Ptr ptr;
    // ...
}

and thus

AsyncFactory<Interf, UseUnique> fac;
fac.reload<Concrete>();

Complete working code here: https://godbolt.org/g/L2511J

The structs should maybe be in a separate namespace, and could probably have better names. Left as an exercise to the reader :)

Max Langhof
  • 23,383
  • 5
  • 39
  • 72