0

如何在编译时找到取消引用后得到的类型?

#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* >.

4

5 回答 5

3

我想是这样的

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

编辑:

不确定上面是否会编译,但这应该可以

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

双重编辑:

是的,我应该在建议之前尝试编译... 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
于 2013-05-15T17:33:17.267 回答
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;
};
于 2013-05-15T17:41:51.730 回答
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
于 2013-05-15T17:30:15.693 回答
1

这应该有效:

typedef typename TIteratorToPointerContainer::value_type TPointer ;
typedef typename TPointer::element_type TValue ;
于 2013-05-15T17:49:25.093 回答
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;

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

于 2013-05-16T10:58:17.167 回答