我已經定義以下模板,用於組合已經定義謂詞:錯誤:預期'||'之前的不合格id令牌
namespace SomeNamespace
{
//TODO: for now simply taking argument type of first predicate
template<typename LhPredicate, typename RhPredicate>
struct OrPredicate : public std::unary_function<typename LhPredicate::argument_type, bool>
{
public:
OrPredicate(LhPredicate const& lh, RhPredicate const& rh)
: m_lh(lh),
m_rh(rh)
{
}
bool operator()(typename LhPredicate::argument_type arg) const
{
return m_lh(arg) || m_rh(arg);
}
private:
LhPredicate m_lh;
RhPredicate m_rh;
};
//TODO: for now simply taking argument type of first predicate
template<typename LhPredicate, typename RhPredicate>
struct AndPredicate : public std::unary_function<typename LhPredicate::argument_type, bool>
{
public:
AndPredicate(LhPredicate const& lh, RhPredicate const& rh)
: m_lh(lh),
m_rh(rh)
{
}
bool operator()(typename LhPredicate::argument_type arg) const
{
return m_lh(arg) && m_rh(arg);
}
private:
LhPredicate m_lh;
RhPredicate m_rh;
};
template<typename LhPredicate, typename RhPredicate>
OrPredicate<LhPredicate, RhPredicate> or(LhPredicate const& lh, RhPredicate const& rh)
{
return OrPredicate<LhPredicate, RhPredicate>(lh, rh);
}
template<typename LhPredicate, typename RhPredicate>
AndPredicate<LhPredicate, RhPredicate> and(LhPredicate const& lh, RhPredicate const& rh)
{
return AndPredicate<LhPredicate, RhPredicate>(lh, rh);
}
}
的問題是,利用輔助函數模板編譯代碼時(或/和),GCC抱怨這些行:
AndPredicate<LhPredicate, RhPredicate> and(LhPredicate const& lh, RhPredicate const& rh)
OrPredicate<LhPredicate, RhPredicate> or(LhPredicate const& lh, RhPredicate const& rh)
這樣的:
error: expected unqualified-id before '||' token
error: expected unqualified-id before '&&' token
所以他其實在抱怨那些行:
return m_lh(arg) && m_rh(arg);
return m_lh(arg) || m_rh(arg);
模板參數(要組合的謂詞)當然正確地定義了operator()本身,我真的不知道gcc的問題是什麼 - 相同的代碼在VS2005上編譯就好了。
任何幫助將高度讚賞。
'和'和'或'保留關鍵字。他們synonims&&和||運營商 – Andrew 2012-07-16 13:23:58