我定义了以下模板,用于组合已定义的谓词:
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 上编译就好了。
任何帮助将不胜感激。