C++ 如何从对象内部的typedef获取模板参数类型
我有以下资料:C++ 如何从对象内部的typedef获取模板参数类型,c++,c++11,templates,C++,C++11,Templates,我有以下资料: template<typename T> struct foo { typedef T type; }; foo<int> real; foo<int>& a = real; a.type b; decltype(a.type) c; a::type c; decltype(a::type) d; 但是它们都不起作用…对于你想要的fooa:decltype(a)::type e 编辑后,对于所需的foo&a: #inclu
template<typename T>
struct foo {
typedef T type;
};
foo<int> real;
foo<int>& a = real;
a.type b;
decltype(a.type) c;
a::type c;
decltype(a::type) d;
但是它们都不起作用…对于你想要的fooa
:decltype(a)::type e代码>
编辑后,对于所需的foo&a
:
#include <type_traits>
std::decay<decltype(a)>::type::type e;
#包括
标准::衰变::类型::类型e;
这是因为在后一种情况下,decltype(a)
是foo&
,因此您首先需要删除引用(这是decay
所做的一部分)以获得底层类型。如果我有foo&a=b代码>-意思是引用?@onqtam:然后你会问一个单独的问题:-)@onqtam:(或者使用std::decation\t(decltype(a))::type
)@onqtam:除非我们不打算整晚玩这个游戏。想一想解决问题需要了解的实际代表性情况,然后确保提出该问题。你想以这样一种方式问一个问题:答案对解决你的实际问题很有用。编辑-我的真实情况正是这样-而decation\t
的东西不起作用(VS2017)。