2010-08-16 4 views
0

부스트를 사용하여 배열 (T)의 첨자 연산자에 의해 반환 된 유형을 결정하려면 어떤 형식 시그니처를 사용해야합니까? 이 배열은 typedef를 포함하지 않으며 제 3자인 배열을 사용합니다.부스트가있는 주어진 배열 유형의 첨자 연산자의 반환 유형을 어떻게 결정합니까?

예. 나는 그것을 판단 할 :

SomeArray<int> tmp(1); 
int& somevalue = tmp[0]; //would equate 
typename subscript_result<SomeArray<int> >::type somevalue = tmp[0]; 

뭔가

template<class T> 
struct subscript_result 
{ 
    typedef boost::result_of<T::operator[](typename T::difference_type)>::type type; 
}; 

처럼? 나는 항상 유형 서명에서 운영자 []와 문제가있었습니다. : |

감사합니다.

답변

0

아마도 당신이 BOOST_TYPEOF/BOOST_TYPEOF_TPL을 사용할 수 http://www.boost.org/doc/libs/1_35_0/doc/html/typeof/refe.html#typeof.typo

BOOST_TYPEOF(tmp[0]) i; 

를 C++ 0X, 당신은 코멘트에 대한 답변에서 decltype(tmp[0]) i;


을 사용할 수 있어야합니다. 아마 당신은 그런 일에 CONST 및 참조를 제거하지 그것을 속일 수

#include <boost/typeof/typeof.hpp> 

template <class T> 
struct identity 
{ 
    typedef T type; 
}; 

template <class T> 
struct subscript_result 
{ 

    template <class Result, class Obj, class Arg> 
    static identity<Result> get_subscript_type(Result (Obj::*)(Arg)); 

    typedef BOOST_TYPEOF(get_subscript_type(&T::operator[])) aux; 
    typedef typename aux::type type; 
}; 

#include <vector> 
#include <iostream> 

template <class Container> 
void foo(Container& c) 
{ 
    typename subscript_result<Container>::type t = c[0]; 
    ++t; 
} 

int main() 
{ 
    //prove that foo gets a reference to vector<int> 
    std::vector<int> vec(1); 
    foo(vec); 
    std::cout << vec[0] << '\n'; 
} 

당신은 아마 또한 배열/포인터에 대한 전문에서 const를 오버로드를 위해 무언가뿐만 아니라 던져 가지고 올해야합니다.

+0

BOOST_TYPEOF/_TPL에 결과의 일부로 참조 성 및 CV 자격이 포함 된 경우 유용 할 것 같습니다. 너무 가깝습니다. – Geoff

+0

@Geoff는 참조 성 및 CV 자격을 찾기 위해 type_traits'is_const','is_volatile' 및'is_reference'를 사용할 수 없습니까? typedef typename const BOOST_TYPEOF (get_subscript_type (& T :: operator [])) :: type type;'typedef typename BOOST_TYPEOF (get_subscript_type (& T :: operator [])) 템플릿 정의의 일부로이를 사용하십시오.) :: type & type;'? – KitsuneYMG

관련 문제