2010-01-29 65 views
1

我有一個奇怪的問題,模板和命名空間...奇怪模板命名空間問題

我有下面的代碼編譯好..

using namespace boost::multi_index; 

template < typename OT, typename KT, KT (OT::* KM)() const, typename KC, typename CMP > 
class OrderBook 
{ 
public: 
    OrderBook() {} 
    ~OrderBook() {} 

    typedef multi_index_container< 
     OT, 
     indexed_by< 
      ordered_unique< 
       const_mem_fun< OT, KT, KM >, 
       KC 
      >, 
      ordered_unique< 
       identity<OT>, 
       CMP 
      > 
     > 
    > Container; 

    typedef typename Container::template nth_index<0>::type index_0; 
    typedef typename Container::template nth_index<1>::type index_1; 

    typedef typename index_0::const_iterator const_iterator_0; 
    typedef typename index_1::const_iterator const_iterator_1; 

    const_iterator_0 begin0() const { return _container.get<0>().begin(); } 
    const_iterator_0 end0() const { return _container.get<0>().end(); } 


public: 
    Container _container; 
}; 

然而,由於命名空間衝突時我插入此代碼到另一個項目,我必須有...(請注意如何我不得不刪除使用namespace boost::multi_index的和手動指定需要的地方

template < typename OT, typename KT, KT (OT::* KM)() const, typename KC, typename CMP > 
class OrderBook 
{ 
public: 
    OrderBook() {} 
    ~OrderBook() {} 

    typedef boost::multi_index::multi_index_container< 
     OT, 
     boost::multi_index::indexed_by< 
      boost::multi_index::ordered_unique< 
       boost::multi_index::const_mem_fun< OT, KT, KM >, 
       KC 
      >, 
      boost::multi_index::ordered_unique< 
       boost::multi_index::identity<OT>, 
       CMP 
      > 
     > 
    > Container; 

    typedef typename Container::template nth_index<0>::type index_0; 
    typedef typename Container::template nth_index<1>::type index_1; 

    typedef typename index_0::const_iterator const_iterator_0; 
    typedef typename index_1::const_iterator const_iterator_1; 

    const_iterator_0 begin0() const { return _container.get<0>().begin(); } 
    const_iterator_0 end0() const { return _container.get<0>().end(); } 


public: 
    Container _container; 
}; 

這給了我牛逼他跟隨g ++的錯誤。

In member function 'typename boost::multi_index::multi_index_container<OT, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::const_mem_fun<OT, KT, KM>, KC, mpl_::na>, boost::multi_index::ordered_unique<boost::multi_index::identity<Value>, CMP, mpl_::na>, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, std::allocator<_CharT> >::nth_index<0>::type::const_iterator OrderBook<OT, KT, KM, KC, CMP>::begin0() const': 

error: expected primary-expression before ')' token 


In member function 'typename boost::multi_index::multi_index_container<OT, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::const_mem_fun<OT, KT, KM>, KC, mpl_::na>, boost::multi_index::ordered_unique<boost::multi_index::identity<Value>, CMP, mpl_::na>, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, std::allocator<_CharT> >::nth_index<0>::type::const_iterator OrderBook<OT, KT, KM, KC, CMP>::end0() const': 

error: expected primary-expression before ')' token 

對不起,長的錯誤消息,我沒有考慮清潔起來,但我想我最好還是留在他們的情況下完整,我刪除的東西是至關重要的。

我想這...

typedef typename Container::template boost::multi_index::nth_index<0>::type index_0; 
typedef typename Container::template boost::multi_index::nth_index<1>::type index_1; 

,它只是由G ++甚至茜草:(

任何想法?

+0

您正在使用哪種版本的g ++?與VC++ 2005 BTW完美兼容的例子。 – 2010-01-29 14:39:17

+0

對於依賴名稱,VC8相當寬鬆,您不必在標準要求您輸入的'typename'和'template'前綴。 – 2010-01-29 14:41:16

+0

這是一個相當古老的gcc 4.1.2。 – ScaryAardvark 2010-01-29 16:39:52

回答

6

前綴get<0>()template

const_iterator_0 begin0() const { return _container.template get<0>().begin(); } 
const_iterator_0 end0 () const { return _container.template get<0>().end(); } 

typename爲受限類型類似,依賴模板必須由template前綴:

struct X { 
    template<class T> void f(); 
}; 

template<class T> 
void test() { 
    T::f<int>(); // ill-formed 
    T::template f<int>(); // ok 
} 

// ... 
test<X>(); 

而對於好奇,那就是§14.2/ 4

當成員模板名稱 專業化後出現。或 - > 在一個後綴表達式,或之後 嵌套名稱說明符在 合格-ID,和 後綴表達式或合格-ID 明確地依賴於 模板參數(14.6.2), 成員模板名稱必須以關鍵字模板作爲前綴 。否則, 名稱被假定爲命名爲 非模板。

+0

謝謝。修復它..我可以問你是如何發現這一點或你使用什麼參考。禁止在任何地方粘貼「模板」一詞,我會_NEVER_發現這一點.... – ScaryAardvark 2010-01-29 14:43:25

+0

ScaryAardvark,我讓gcc指向問題所在的實際行,並注意到您引用了_container模板成員函數,並且_container是模板參數類型。依賴模板必須以'template'作爲前綴,與依賴類型的'typename'類似。 – 2010-01-29 14:50:09

+0

更正,我的意思是'_container'實際上是一個模板參數相關類型 - 如果這是正確的術語。 – 2010-01-29 15:28:11

0

也許我可以猜測,其中的一些功能不在boost::multi_index命名空間:indexed_bordered_uniqueconst_mem_funidentity

+0

不,他們都在boost :: multi_index命名空間..我需要指定它們,因爲我有一個與「身份」重複。 – ScaryAardvark 2010-01-29 14:42:06