如何在编译时找到取消引用后得到的类型?
#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* >
.