我有ModeSessions类和2个派生类:ReadModeSessions和WriteModeSessions; ModeSessions具有唯一的Mode类型。 在Logic中,我使用自己的唯一类型在容器中收集每个会话。 但是现在我坚持使用模板方法,以通过给定的模式返回正确的会话。
#include <QVector>
#include <QSharedPointer>
namespace states {
enum class Mode{ read1, read2, write1, write2};
constexpr bool modeIsReadType(Mode m){ return (m == Mode::read1 || m == Mode::read2)? true: false;}
constexpr bool modeIsWriteType(Mode m){ return (m == Mode::write1 || m == Mode::write2)? true: false;}
}
class ModeSession{
public:
states::Mode type;
};
using namespace states;
class ReadModeSession: public ModeSession{
public:
ReadModeSession(Mode m){ModeSession::type = m;}
};
class WriteModeSession : public ModeSession{
public:
WriteModeSession(Mode m){ModeSession::type = m;}
};
class Logic
{
public:
Logic(){
allModeSessions << QSharedPointer<ReadModeSession>(new ReadModeSession(Mode::read1));
allModeSessions << QSharedPointer<ReadModeSession>(new ReadModeSession(Mode::read2));
allModeSessions << QSharedPointer<WriteModeSession>(new WriteModeSession(Mode::write1));
allModeSessions << QSharedPointer<WriteModeSession>(new WriteModeSession(Mode::write2));
}
QVector<QSharedPointer<ModeSession>> allModeSessions;
template<class Tm> QSharedPointer<ReadModeSession>
getMs(Tm m, typename std::enable_if<states::modeIsReadType(m), Tm>::type){
for (QSharedPointer<ModeSession> ms: allModeSessions){
if (ms->type == m) return ms.dynamicCast<ReadModeSession>();
}
return nullptr;
}
template<class Tm> QSharedPointer<WriteModeSession>
getMs(Tm m, typename std::enable_if<states::modeIsWriteType(m), Tm>::type){ //to stack overflow....
for (QSharedPointer<ModeSession> ms: allModeSessions){
if (ms->type == m) return ms.dynamicCast<WriteModeSession>();
}
return nullptr;
}
};
两种模板方法都具有
In file included from ..\enableIf\logic.cpp:1:0:
..\enableIf\logic.h:35:74: error: template argument 1 is invalid
getMs(Tm m, typename std::enable_if<states::modeIsReadType(m), Tm>::type){
^
..\enableIf\logic.h:35:74: error: template argument 1 is invalid
..\enableIf\logic.h:35:74: error: template argument 1 is invalid
..\enableIf\logic.h:35:74: error: template argument 1 is invalid
..\enableIf\logic.h:35:35: error: invalid use of template-name 'std::enable_if' without an argument list
getMs(Tm m, typename std::enable_if<states::modeIsReadType(m), Tm>::type){
^
..\enableIf\logic.h:35:44: error: expected ',' or '...' before '<' token
getMs(Tm m, typename std::enable_if<states::modeIsReadType(m), Tm>::type){
^
..\enableIf\logic.h:42:75: error: template argument 1 is invalid
getMs(Tm m, typename std::enable_if<states::modeIsWriteType(m), Tm>::type){ //to stack overflow....
^
..\enableIf\logic.h:42:75: error: template argument 1 is invalid
..\enableIf\logic.h:42:75: error: template argument 1 is invalid
..\enableIf\logic.h:42:75: error: template argument 1 is invalid
..\enableIf\logic.h:42:35: error: invalid use of template-name 'std::enable_if' without an argument list
getMs(Tm m, typename std::enable_if<states::modeIsWriteType(m), Tm>::type){ //to stack overflow....
^
..\enableIf\logic.h:42:44: error: expected ',' or '...' before '<' token
getMs(Tm m, typename std::enable_if<states::modeIsWriteType(m), Tm>::type){ //to stack overflow....
我不确定是否做对了,尝试将constexpr函数放进去,但是据我了解,应该有bool参数,下面的示例包含使用bool作为返回值的std :: base_of或std :: is_integer。 ,有什么区别?
我可能会考虑另一种方法,但是重点是,我需要关心具有一些通用方法并且对于每种类型都是唯一的会话。 (也许要去掉那些方法才能在另一个新的sessionManager类中实现它)