我定义了以下模板,用于组合已定义的谓词:
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上的相同代码编译就好了。
任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
and
和or
都是C ++的关键字。你介意为他们换名字吗?
答案 1 :(得分:1)
and
和or
保留keywords
。它们是&&
和||
运算符的同义词。例如:
bool or(int a)
{
}
不会编译