2

이 예제의 자세한 내용은 사과드립니다. 프로젝트에서 고안했습니다. 주석 처리 된 항목 1과 항목 2는 다음 코드에서 중요합니다.C++ 템플릿 meta-magic, 템플릿 호출 사이트 자격 공제 메커니즘

는 내가이 const_iterator를의 모든 것에 대한 반복자가 잘 작동, 또는 경우 전환하면 범위에서 함수가 CONST이, 내 회원 용기 (항목 2)에 const가 아닌 itereator을받을 수 없어

나는 바깥 쪽 기능을

#include <boost/intrusive/set.hpp> 

struct x : public boost::intrusive::set_base_hook<> 
{ 
    int y; 
}; 

struct finder 
{ 
    bool operator()(const int & i, const x & _x) const 
    { return i < _x.y;} 
    bool operator()(const x & _x, const int & i) const 
     { return _x.y < i;} 
}; 

class trampoline 
{ 
public: 
    void bounce() const /* item 1 */ 
    { 
     /*item 2*/ 
     boost::intrusive::set<x>::iterator it = _set.find(1,finder()); 
    } 

    boost::intrusive::set<x> _set; 
}; 

int main() 
{ 
    trampoline t; 
    t.bounce(); 
} 
비 const 그것도 잘 작동합니다. + 결국 다음과 같은 템플릿 정의를 알려준

test.cc: In member function ‘void trampoline::bounce() const’:test.cc:21: error: conversion from ‘boost::intrusive::tree_iterator<boost::intrusive::rbtree_impl<boost::intrusive::setopt<boost::intrusive::detail::base_hook_traits<x, boost::intrusive::rbtree_node_traits<void*, false>, (boost::intrusive::link_mode_type)1u, boost::intrusive::default_tag, 3>, std::less<x>, long unsigned int, true> >, true>’ to non-scalar type ‘boost::intrusive::tree_iterator<boost::intrusive::rbtree_impl<boost::intrusive::setopt<boost::intrusive::detail::base_hook_traits<x, boost::intrusive::rbtree_node_traits<void*, false>, (boost::intrusive::link_mode_type)1u, boost::intrusive::default_tag, 3>, std::less<x>, long unsigned int, true> >, false>’ requested

(/include/boost/intrusive/detail/tree_node.hpp : 리버스 엔지니어링의 시간 다음과 같은 오류 메시지에서 문제 이후 지금 완벽한 이해를, 만듭니다 72) :

///////////////////////////////////////////////////////////////////////////// 
//                   // 
//     Implementation of the tree iterator     // 
//                   // 
///////////////////////////////////////////////////////////////////////////// 

// tree_iterator provides some basic functions for a 
// node oriented bidirectional iterator: 
template<class Container, bool IsConst> 

이 .... 도대체 그 템플릿은 포함하는 함수의 callsite에서 IsConst을 통과됩니까 어떻게

그것이 내가 조금 후에 문제를 해결 말할 충분했다? 나의 두뇌는 폭발 할 준비가되어있다. (내가 알고있는 모든 것이 단순하지만, 나는 신비화된다.) 역학을 설명하기위한 단계별 구현에 대한 자세한 설명이 도움이 될 것입니다.

나는 비슷한 질문을 here 비슷한 방식으로 C++의 템플릿 메커니즘의 형식 공제/앨리어싱과 관련하여 사용합니다. 참고 문헌은 높이 평가되지만 지식에 대한 지식이 필요합니다. D. 이 질문에 대한 인내심이 있다면 다른 질문에 대한 설교를 시도 할 수도 있습니다.

답변

1

boost::intrusive::set의 find() 멤버 함수에는 const 및 non-const 과부하가 있습니까? 내 말은, 그게 내가 어떻게 할거야 :

template <typename T /*...*/ > 
class set { 
    //... 
    public: 
    template <bool isConst> 
    class iterator { 
     //.... 
    }; 
    iterator<true> find(/*..*/) const; //notice the const 
    iterator<false> find(/*..*/);  //notice no const 
}; 

이것은 정말 좋은 옛 패션 const-correctness입니다.

+0

어서, 그렇게 간단하지 않을 수 있습니다, 내 뇌가 폭발 할 준비가 된 것은 당연합니다. 나는 오래된 학교 다. 모든 것이 공허한 형의 사람 = D 다. 그래서 const 과부하는 컴파일러 = D에 의해 멤버 객체에서 선택되고 유형 에일리어싱을 위반하여 인공 const 구문을 적용합니다 (--i.e., 유효한 복사 생성자가 아닙니다). 나는 "그들의 C++의 반환 유형 다형성은 내 머리 속에 달려있다"라는 혼란을 겪고있었습니다. –

+0

함수가 호출되는 객체 인스턴스를 함수 호출의 첫 번째 (숨겨진) 매개 변수로 생각하면 오버로드 메커니즘에 영향을 미칩니다. 멤버 함수의 cv-qualifier는이 첫 번째 (숨겨진) 매개 변수의 cv- 자격으로 간주되어야합니다. 컴파일러는 가장 일치하는 오버로드를 찾습니다. 따라서 비 const 객체는 더 나은 일치하는 오버로드 된 버전이므로 사용할 수있는 경우 비 const 함수를 호출합니다. –

1

그런가요?

Foo<false> bar(); 
Foo<true> bar() const;