如何从对象内部的typedef获取模板参数类型

onq*_*tam 0 c++ templates c++11

我有以下内容:

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

foo<int> real;
foo<int>& a = real;
Run Code Online (Sandbox Code Playgroud)

我希望得到模板类型a- 这可能吗?我尝试过以下方法:

a.type b;
decltype(a.type) c;
a::type c;
decltype(a::type) d;
Run Code Online (Sandbox Code Playgroud)

但它们都不起作用......

Ker*_* SB 8

对于foo<int> a你想要的:decltype(a)::type e;

编辑后,foo<int>& a您想要:

#include <type_traits>

std::decay<decltype(a)>::type::type e;
Run Code Online (Sandbox Code Playgroud)

那是因为在后一种情况下,decltype(a)foo<int>&,所以你首先需要删除引用(这是什么的一部分decay)来获取底层类型.