简体   繁体   English

具有继承父级函数的 C++ 类模板

[英]C++ Class template with functions from inherit parents

Lets assume this class using C++14 (and QtRO system in origin).让我们假设这个类使用 C++14(和 QtRO 系统)。 https://pastebin.com/EVT4XJiz https://pastebin.com/EVT4XJiz

#include <iostream>
#include <functional>
#include <memory>
 
class QTRO_1Source {
public:
    void hvEnabledChanged(bool) {};
    virtual bool hvEnabled() const = 0;
};
 
class QTRO_1SimpleSource : public QTRO_1Source {
public:
    bool hvEnabled() const override { return false; }
};
 
class QTRO_1 : public QTRO_1SimpleSource {
public:
    void write_hvEnabled(bool/*enabled*/) { }
};
 
 
template<typename ReplicaType, typename ValueType>
class ProxyProperty
{
public:
    using ReadFuncPtr = ValueType (ReplicaType::*)() const;
    using WriteFuncPtr = void (ReplicaType::*)(ValueType);
    using ChangeSigFuncPtr = void (ReplicaType::*)(ValueType);
    using UpdateHandlerFunctor = std::function<void(ValueType value)>;
    ProxyProperty(ReplicaType *replica,
                  ReadFuncPtr readFunc,
                  WriteFuncPtr writeFunc,
                  ChangeSigFuncPtr changeSignal,
                  UpdateHandlerFunctor updateHandler)
    {
        m_replica = replica;
        m_readFunc = readFunc;
        m_writeFunc = writeFunc;
        m_changeSignal = changeSignal;
        m_updateHandler = updateHandler;
    }
    template<typename T, typename HandlerT>
    void write(T value, HandlerT handler) {
        (m_replica->*m_writeFunc)(value);
    }
private:
    ReplicaType *m_replica;
    ReadFuncPtr m_readFunc;
    WriteFuncPtr m_writeFunc;
    ChangeSigFuncPtr m_changeSignal;
    UpdateHandlerFunctor m_updateHandler;
    int m_timeout{3000};
};
 
template<typename ReplicaType, typename ValueType, typename UpHandlerT>
auto createProxyProperty(ReplicaType *replica,
                         ValueType (ReplicaType::*readFunc)() const,
                         void (ReplicaType::*writeFunc)(ValueType),
                         void (ReplicaType::*changeSignal)(ValueType),
                         UpHandlerT upHandler)
{
    return std::make_unique<ProxyProperty<ReplicaType,ValueType> >(replica, readFunc, writeFunc, changeSignal, upHandler);
}
 
int main(int argc, char *argv[]){
    QTRO_1 model;
//    static auto p = createProxyProperty<QTRO_1, bool>(
    static auto p = createProxyProperty(
                &model,
                &QTRO_1::hvEnabled,
                &QTRO_1::write_hvEnabled,
                &QTRO_1::hvEnabledChanged,
        [](auto val) { std::cout<< "COSTAM"; }
    );
}

Now, when I'm using it with first line (giving both types) - it works.现在,当我将它与第一行一起使用时(提供两种类型) - 它有效。 When I move type deduction on compiler - I receive this error error.当我在编译器上移动类型推导时 - 我收到此错误错误。

error: no matching function for call to 'createProxyProperty(QTRO_1*, bool (QTRO_1SimpleSource:: )() const, void (QTRO_1:: )(bool), void (QTRO_1Source::*)(bool), main(int, char**)::<lambda(auto:1)>)' 76 |错误:没有匹配的函数调用'createProxyProperty(QTRO_1*, bool (QTRO_1SimpleSource:: )() const, void (QTRO_1:: )(bool), void (QTRO_1Source::*)(bool), main(int, char **)::<lambda(auto:1)>)' 76 | ); );

Yes, all 3 methods are from different class in inheritance tree ( QTRO_1 from QTRO_1SimpleSource from QTRO_1Source ).是的,所有 3 个方法都来自继承树中的不同类( QTRO_1来自QTRO_1SimpleSource来自QTRO_1Source )。 I could - of course - wrap those methods in std::function.我当然可以将这些方法包装在 std::function 中。
I would like to make compiler deduce types for me.我想让编译器为我推断类型。 How to do it?怎么做?

PS: Added minimal reproduction code. PS:添加了最少的复制代码。

ReplicaType gets deduced from 4 different arguments, and the results of deduction are conflicting. ReplicaType是从 4 个不同的参数中推导出来的,推导出的结果是相互矛盾的。 See compiler output:查看编译器输出:

note: candidate template ignored: deduced conflicting types for parameter 'ReplicaType' ('QTRO_1' vs. 'QTRO_1SimpleSource')注意:候选模板被忽略:推导出参数“ReplicaType”的冲突类型(“QTRO_1”与“QTRO_1SimpleSource”)

You can workaround it by employing 3 additional template parameter for class types in 2-4 function parameters:您可以通过在 2-4 个函数参数中为类类型使用 3 个附加模板参数来解决它:

template<typename ReplicaType, typename ValueType, typename UpHandlerT,
         class C1, class C2, class C3>
auto createProxyProperty(ReplicaType *replica,
                         ValueType (C1::*readFunc)() const,
                         void (C2::*writeFunc)(ValueType),
                         void (C3::*changeSignal)(ValueType),
                         UpHandlerT upHandler)
{
    return std::make_unique<ProxyProperty<ReplicaType,ValueType> >(replica, readFunc, writeFunc, changeSignal, upHandler);
}

See full example here .请参阅此处的完整示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM