簡體   English   中英

用於類構建的 2 個模板化構造函數與可變參數模板的組合。 如何?

[英]Combination of 2 templated constructors for class build with variadic templates. How?

我創建了一些小型工廠類。 請參閱下面的代碼。 工廠使用帶有鍵和類創建器功能的簡單映射。 挑戰在於我想為 createInstance 函數使用不同的簽名。 這樣,我可以使用具有不同構造函數簽名的不同派生類。 因此,我將能夠將不同的值傳遞給類的構造函數。

由於我無法在std::function和最終在std::map存儲具有不同簽名的std::function ,因此我編寫了一個簡短的包裝器來“擦除”類型參數部分。 所以,基本上我可以將函數存儲在std::function ,最后存儲在std::any

該代碼工作正常。 您可以編譯它並毫無問題地運行它。

還附上了一些測試代碼。

現在我的問題:

我試圖在我的類"Creator" 中只提出一個構造函數。 但我無法做到這一點。 在我看來,這應該是可能的。 我想請求您的支持以提出可能的解決方案。

請注意:我正處於重構階段。 所以代碼還不是“完美”的。

#include <iostream>
#include <map>
#include <utility>
#include <functional>
#include <any>

// Some demo classes ----------------------------------------------------------------------------------
struct Base {
    Base(int d) : data(d) {};
    virtual ~Base() { std::cout << "Destructor Base\n"; }
    virtual void print() { std::cout << "Print Base\n"; }
    int data{};
};
struct Child1 : public Base {
    Child1(int d, std::string s) : Base(d) { std::cout << "Constructor Child1 " << d << " " << s << "\n"; }
    virtual ~Child1() { std::cout << "Destructor Child1\n"; }
    virtual void print() { std::cout << "Print Child1: " << data << "\n"; }
};
struct Child2 : public Base {
    Child2(int d, char c, long l) : Base(d) { std::cout << "Constructor Child2 " << d << " " << c << " " << l << "\n"; }
    virtual ~Child2() { std::cout << "Destructor Child2\n"; }
    virtual void print() { std::cout << "Print Child2: " << data << "\n"; }
};
struct Child3 : public Base {
    Child3(int d, long l, char c, std::string s) : Base(d) { std::cout << "Constructor Child3 " << d << " " << l << " " << c << " " << s << "\n"; }
    virtual ~Child3() { std::cout << "Destructor Child3\n"; }
    virtual void print() { std::cout << "Print Child3: " << data << "\n"; }
};


using Ret = std::unique_ptr<Base>;

template <typename ... Args>
using Func = std::function<Ret(Args...)>;

// ---------------------------------------------------------------------------------------------------
// Hide away the different signatures for std::function
class Creator
{
public:
    Creator() {}

    // I want to combine the follwong 2 constructors in one
    template<typename Function>
    Creator(Function&& fun) : Creator(std::function(fun)) {}
    template<typename ... Args>
    Creator(Func<Args...> fun) : m_any(fun) {}

    template<typename ... Args>
    Ret operator()(Args ... args)   { return std::any_cast<Func<Args...>>(m_any)(args...);  }
protected:
    std::any m_any;
};


template <class Child, typename ...Args>
std::unique_ptr<Base> createClass(Args...args) { return std::make_unique<Child>(args...); }

// The Factory ----------------------------------------------------------------------------------------
template <class Key>
class Factory
{
    std::map<Key, Creator> selector;
public:
    Factory(std::initializer_list<std::pair<const Key, Creator>> il) : selector(il) {}

    template <typename ... Args>
    Ret createInstance(Key key, Args ... args) {
        if (selector.find(key) != selector.end()) {
            return selector[key](args ...);
        }
        else {
            return std::make_unique<Base>(0);
        }
    }
};

int main()
{
    Factory<int> factory { 
        {1, createClass<Child1, int, std::string>},
        {2, createClass<Child2, int, char, long>},
        {3, createClass<Child3, int, long, char, std::string>}
    };

    // Some test values
    std::string s1(" Hello1 "); std::string s3(" Hello3 ");
    int i = 1;  const int ci = 1;   int& ri = i;    const int& cri = i;   int&& rri = 1;

    std::unique_ptr<Base> b1 = factory.createInstance(1,   1, s1);
    std::unique_ptr<Base> b2 = factory.createInstance(2,   2, '2', 2L);
    std::unique_ptr<Base> b3 = factory.createInstance(3,   3, 3L, '3', s3);

    b1->print();
    b2->print();
    b3->print();
    b1 = factory.createInstance(2, 4, '4', 4L);
    b1->print();
    return 0;
}

所以,再次,我想擺脫 2 個構造函數

    template<typename Function>
    Creator(Function&& fun) : Creator(std::function(fun)) {}

    template<typename ... Args>
    Creator(Func<Args...> fun) : m_any(fun) {}

並且最后只有一個。 如何?

抱歉代碼太長。

簡單地刪除采用std::function的構造std::function怎么樣?

template<typename Function>
Creator(Function&& fun) : m_any(std::function(fun)) {}

只有當它不是std::function它才會包裝fun 對於如果它是std::function那么它就是一個 noop。

活生生的例子


查看您的代碼,您只使用函數指針作為工廠函數。 您可以直接使用函數指針而不是std::function為自己節省一些間接std::function ,它已經是像std::any這樣的層。

template <typename ... Args>
using Func = std::add_pointer_t<Ret(Args...)>;

// in you class:

template<typename Function, std::enable_if_t<std::is_pointer_v<Function>, int> = 0>
Creator(Function fun) : m_any(fun) {}

如果你使用它,在 C++20 中你可以刪除丑陋的 enable if 並使用一個概念:

template<typename T>
concept FunctionPointer = requires(T t) {
    requires std::is_pointer_v<T>;
    requires std::is_function_v<std::remove_pointer_t<T>>;
    { std::function(t) };
};

// then in your class:

Creator(FunctionPointer auto fun) : m_any(fun) {}

活生生的例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM