找出汽车的类型
find out the type of auto
我在C++1y中玩泛型lambda,我经常被不知道auto
变量/参数的类型弄糊涂。有什么好办法找到它吗?
目前我正在使用typeid(decltype(arg)).name())
,但它不是很有用@编码给出了一个稍微好一点的结果,但仍然很难破译它
示例:
auto f = [](auto && a, auto b) {
std::cout << std::endl;
std::cout << typeid(decltype(a)).name() << std::endl << @encode(decltype(a)) << std::endl;
std::cout << typeid(decltype(b)).name() << std::endl << @encode(decltype(b)) << std::endl;
};
int i = 1;
f(i, i);
f(1, 1);
f(std::make_unique<int>(2), std::make_unique<int>(2));
auto const ptr = std::make_unique<int>();
f(ptr, nullptr);
输出
i // it does not tell me it is reference
^i // ^ means pointer, but it is actually reference, kinda pointer though
i
i
i
^i
i
i
NSt3__110unique_ptrIiNS_14default_deleteIiEEEE
^{unique_ptr<int, std::__1::default_delete<int> >={__compressed_pair<int *, std::__1::default_delete<int> >=^i}}
NSt3__110unique_ptrIiNS_14default_deleteIiEEEE
{unique_ptr<int, std::__1::default_delete<int> >={__compressed_pair<int *, std::__1::default_delete<int> >=^i}}
NSt3__110unique_ptrIiNS_14default_deleteIiEEEE
r^{unique_ptr<int, std::__1::default_delete<int> >={__compressed_pair<int *, std::__1::default_delete<int> >=^i}}
Dn
*
我主要想知道的是通过值等传递的参数是什么。
我使用的是Xcode 5.1.1
使用GCC的__cxa_demangle
函数:
std::string demangled(std::string const& sym) {
std::unique_ptr<char, void(*)(void*)>
name{abi::__cxa_demangle(sym.c_str(), nullptr, nullptr, nullptr), std::free};
return {name.get()};
}
auto f = [](auto && a, auto b) {
std::cout << demangled(typeid(decltype(a)).name()) << 'n';
std::cout << demangled(typeid(decltype(b)).name()) << 'n';
};
这就是我最终得到的结果。结合@Konrad Rudolph的回答和@Joachim Pileborg的评论
std::string demangled(std::string const& sym) {
std::unique_ptr<char, void(*)(void*)>
name{abi::__cxa_demangle(sym.c_str(), nullptr, nullptr, nullptr), std::free};
return {name.get()};
}
template <class T>
void print_type() {
bool is_lvalue_reference = std::is_lvalue_reference<T>::value;
bool is_rvalue_reference = std::is_rvalue_reference<T>::value;
bool is_const = std::is_const<typename std::remove_reference<T>::type>::value;
std::cout << demangled(typeid(T).name());
if (is_const) {
std::cout << " const";
}
if (is_lvalue_reference) {
std::cout << " &";
}
if (is_rvalue_reference) {
std::cout << " &&";
}
std::cout << std::endl;
};
int main(int argc, char *argv[])
{
auto f = [](auto && a, auto b) {
std::cout << std::endl;
print_type<decltype(a)>();
print_type<decltype(b)>();
};
const int i = 1;
f(i, i);
f(1, 1);
f(std::make_unique<int>(2), std::make_unique<int>(2));
auto const ptr = std::make_unique<int>();
f(ptr, nullptr);
}
并输出
int const &
int
int &&
int
std::__1::unique_ptr<int, std::__1::default_delete<int> > &&
std::__1::unique_ptr<int, std::__1::default_delete<int> >
std::__1::unique_ptr<int, std::__1::default_delete<int> > const &
std::nullptr_t
我主要想知道的是通过值等传递的参数是什么。
这很容易。
template<class T>
struct is_lvalue:std::false_type {};
template<class T>
struct is_lvalue<T&>:std::true_type {};
template<class T>
struct is_literal:std::true_type {};
template<class T>
struct is_literal<T&&>:std::false_type {};
template<class T>
struct is_literal<T&>:std::false_type {};// I do not think needed
只做CCD_ 4和CCD_。
rvalue
(临时引用或移动引用)是一个非文字的非左值。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 为什么是如果(类型汽车!= 'X' ||"S" ||'L') 的评估值始终为真
- 汽车推断出的那种类型是什么?
- 找出汽车的类型