2012-07-16 89 views
0

我已經定義以下模板,用於組合已經定義謂詞:錯誤:預期'||'之前的不合格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上編譯就好了。

任何幫助將高度讚賞。

+0

'和'和'或'保留關鍵字。他們synonims&&和||運營商 – Andrew 2012-07-16 13:23:58

回答

1

andor保留keywords。他們是&&||運營商synonims。例如:

bool or(int a) 
{ 

} 

不會編譯

+0

似乎有人在學習整個生活:) - 非常感謝一羣絕望的開發人員 – user1528980 2012-07-16 13:40:27

1

andor都是C++的關鍵字。你介意爲他們改名嗎?

相關問題