2010-01-29 5 views
1

나는 템플릿과 네임 스페이스와 이상한 문제 ...이상한 템플릿 네임 스페이스 문제

있어 I가 잘 컴파일 다음 코드 .. 때문에 네임 스페이스 충돌하는 경우에 그러나

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

, 내가 가지고있는 다른 프로젝트에이 코드를 삽입 ... (내가

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

을 필요한 곳에 나에게 t을 제공합니다 어느를 지정 수동으로 사용 namespace boost::multi_index를 제거하고 했어 방법에 주목 그는 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와 완벽하게 호환되는 예제. –

+0

VC8은 의존적 인 이름에 대해서는 꽤 느리다. 표준에서 요구하는 곳에'typename'과'template'을 항상 붙일 필요는 없다. –

+0

합리적으로 오래된 gcc 4.1.2입니다. – ScaryAardvark

답변

6

접두사 get<0>()template으로 :

는 종속 타입 typename 유사
const_iterator_0 begin0() const { return _container.template get<0>().begin(); } 
const_iterator_0 end0 () const { return _container.template get<0>().end(); } 

종속 템플릿 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

+0

ScaryAardvark, gcc가 문제가있는 실제 행을 가리키게하고'_container'의 템플리트 멤버 함수를 참조하고'_container'가 템플리트 인수 유형임을 확인했습니다. 종속 템플릿은'typename'과 유사한'template' 접두사가 있어야합니다. –

+0

수정,'_container'는 템플릿 인자에 의존적 인 타입입니다. 실제로 올바른 용어 인 경우입니다. –

0

어쩌면 이러한 기능 중 일부는에없는 것을 추측 할 수 boost::multi_index 네임 스페이스 : indexed_b, ordered_unique, const_mem_fun, 또는 identity

+0

아니요, 모두 boost :: multi_index 네임 스페이스에 있습니다. "identity"와 중복 되었기 때문에 정확하게 지정해야했습니다. – ScaryAardvark