不确定这是否是 MSVC 2013 终极版中的错误,但它在 2015 社区中运行良好。声明:https ://github.com/dtmoodie/MetaObject/blob/master/include/MetaObject/Signals/TypedSignal.hpp
class IMetaObject;
class Context;
class Connection;
template<class Sig> class TypedSignalRelay;
template<class Sig> class TypedSignal{};
template<class...T> class MO_EXPORTS TypedSignal<void(T...)> : public ISignal
{
std::shared_ptr<Connection> Connect(ISlot* slot);
std::shared_ptr<Connection> Connect(std::shared_ptr<ISignalRelay>& relay);
std::shared_ptr<Connection> Connect(std::shared_ptr<TypedSignalRelay<void(T...)>>& relay);
};
template<class R, class...T> class MO_EXPORTS TypedSignal<R(T...)> : public ISignal
{
std::shared_ptr<Connection> Connect(ISlot* slot);
std::shared_ptr<Connection> Connect(std::shared_ptr<ISignalRelay>& relay);
std::shared_ptr<Connection> Connect(std::shared_ptr<TypedSignalRelay<R(T...)>>& relay);
};
------------- 返回值特化 ---------
template<class R, class...T>
std::shared_ptr<Connection> TypedSignal<R(T...)>::Connect(ISlot* slot)
{
return slot->Connect(this);
}
template<class R, class...T>
std::shared_ptr<Connection> TypedSignal<R(T...)>::Connect(std::shared_ptr<ISignalRelay>& relay)
{
...
}
template<class R, class...T>
std::shared_ptr<Connection> TypedSignal<R(T...)>::Connect(std::shared_ptr<TypedSignalRelay<R(T...)>>& relay)
{
....
}
-------- void 返回特化 -------------
template<class...T>
std::shared_ptr<Connection> TypedSignal<void(T...)>::Connect(ISlot* slot)
{
return slot->Connect(this);
}
template<class...T>
std::shared_ptr<Connection> TypedSignal<void(T...)>::Connect(std::shared_ptr<ISignalRelay>& relay)
{
...
}
template<class...T>
std::shared_ptr<Connection> TypedSignal<void(T...)>::Connect(std::shared_ptr<TypedSignalRelay<void(T...)>>& relay)
{
...
}
在 2013 年出现此错误,但在 2015 年没有投诉:
4>e:\code\metaobject\include\metaobject\signals\detail/TypedSignalImpl.hpp(77): error C2244: 'mo::TypedSignal<R(T...)>::Connect' : unable to match function definition to an existing declaration
4> definition
4> 'std::shared_ptr<mo::Connection> mo::TypedSignal<R(T...)>::Connect(std::shared_ptr<mo::TypedSignalRelay<R(T...)>> &)'
4> existing declarations
4> 'std::shared_ptr<mo::Connection> mo::TypedSignal<R(T...)>::Connect(std::shared_ptr<mo::TypedSignalRelay<R(T...)>> &)'
4> 'std::shared_ptr<mo::Connection> mo::TypedSignal<R(T...)>::Connect(std::shared_ptr<mo::ISignalRelay> &)'
4> 'std::shared_ptr<mo::Connection> mo::TypedSignal<R(T...)>::Connect(mo::ISlot *)'
[编辑] 添加了完整的示例和原始源的链接。