0

既に定義されている述語を結合するために使用される、次のテンプレートを定義しました。

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);
}

}

問題は、ヘルパー関数テンプレート (or/and) を使用してコードをコンパイルするときに、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 で問題なくコンパイルされます。

どんな助けでも大歓迎です。

4

2 に答える 2

1

andorはどちらも C++ のキーワードです。それらの名前を変更してもよろしいですか?

于 2012-07-16T13:24:06.573 に答える
1

andおよびor予約済みkeywordsです。これらは&&and||演算子と同義です。例えば:

bool or(int a)
{

}

コンパイルされません

于 2012-07-16T13:25:22.483 に答える