gpt4 book ai didi

c++ - 如何在取消引用另一种类型后获得你得到的类型?

转载 作者:太空宇宙 更新时间:2023-11-04 14:55:33 24 4
gpt4 key购买 nike

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

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

最佳答案

#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;
};

关于c++ - 如何在取消引用另一种类型后获得你得到的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16571405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com