derefencing另一种类型后如何获得你得到的类型?

时间:2013-05-15 17:28:29

标签: c++ c++11

如何在编译时找到解除引用后的内容?

#include <vector>
#include <memory>

template <class TIteratorToPointerContainer>
class Something
{
public:
    typedef /*the thing you get by dereferencing TIteratorToPointer*/ TPointer;
    typedef /*the thing you get by dereferencing TPointer*/           TValue;
};

int main()
{
    Something<
              typename std::vector< std::shared_ptr<int> >::iterator 
              >::TPointer pointer;
                      // "pointer" is of type std::shared_ptr<int>
    Something< 
              typename std::vector< std::shared_ptr<int> >::iterator 
              >::TValue value;
                    // "value" is of type int
    return 0;
}

我可以使用C ++ 11功能。

来自答案的编辑:

typedef typename TIteratorToPointerContainer::value_type TPointer;
typedef typename TPointer::element_type TValue;

适用于std::vector< std::shared_ptr<int> >但不适用于std::vector< int* >

5 个答案:

答案 0 :(得分:3)

我认为是这样的事情

typedef TIteratorToPointerContainer::value_type TPointer
typedef delctype(*TPointer) TValue

编辑:

不确定以上是否会编译,但这应该有效

typedef TIteratorToPointerContainer::value_type TPointer
typedef TPointer::element_type TValue

DOUBLE-编辑:

是的,我应该在建议之前尝试编译...... http://ideone.com/ByEvXj

#include <vector>
#include <memory>
#include <iostream>
#include <typeinfo>
using namespace std;

template <class TIteratorToPointerContainer>
class Something
{
public:
    typedef typename TIteratorToPointerContainer::value_type TPointer;
    typedef typename TPointer::element_type        TValue;
};

int main()
{
    Something<
              typename std::vector< std::shared_ptr<int> >::iterator 
              >::TPointer pointer;
                      // "pointer" is of type std::shared_ptr<int>
    Something< 
              typename std::vector< std::shared_ptr<int> >::iterator 
              >::TValue value;
                    // "value" is of type int

  std::cout << "pointer-name = " << typeid(pointer).name() << endl;
  std::cout << "value-name = " << typeid(value).name() << endl;
    return 0;
}

输出:

pointer-name = St10shared_ptrIiE
value-name = i

答案 1 :(得分:3)

#include <type_traits>
#include <utility>

template <class TIteratorToPointerContainer>
class Something
{
private:
     using TPointer_ = decltype( *std::declval<TIteratorToPointerContainer>() );
     using TValue_ = decltype( *std::declval<TPointer>() );
public:
    using TPointer = typename std::remove_reference<TPointer_> :: type;
    using TValue = typename std::remove_reference<TValue_> :: type;
};

答案 2 :(得分:1)

尝试:decltype( *ptr )。这应该可以为您提供所需的类型。

如果你没有操纵指针,你可以这样做:

template <typename T>
struct RemovePtr
{
  typedef T type;
}

template <>
struct RemovePtr<T *>
{
  typedef T type;
}

RemovePtr<int *>::type i = 5; // should be of type int

答案 3 :(得分:1)

这应该有效:

typedef typename TIteratorToPointerContainer::value_type TPointer ;
typedef typename TPointer::element_type TValue ;

答案 4 :(得分:1)

不需要C ++ 11。 C ++ 98已经有std::iterator_traits

typedef typename std::iterator_traits<TIteratorToPointer>::value_type TPointer;
typedef typename std::iterator_traits<TPointer>::value_type           TValue;

后者有效,因为指针也是迭代器。