我已经定义以下模板,用于组合已经定义谓词:错误:预期'||'之前的不合格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