找出汽车的类型

编程入门 行业动态 更新时间:2024-10-28 20:25:02
本文介绍了找出汽车的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用C ++ 1y中的通用lambda进行游戏,我常常因不知道 auto 变量/参数的类型而感到困惑。有什么好办法找出来吗?

I am playing with generic lambda in C++1y and I often confused by don't know what is the type of auto variable/parameter. Is any good way to find it out?

当前我正在使用 typeid(decltype(arg))。name()),但不是很有用。 @encode 会给出更好的结果但仍然很难破译

Currently I am using typeid(decltype(arg)).name()) but it is not very useful. @encode gives a slightly better result but still hard to decipher it

示例:

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 *

我主要想要的是知道参数是左值ref /右值ref /通过值等传递。

I mainly want is to know that is the parameter a lvalue ref/rvalue ref/passed by value etc.

我正在使用Xcode 5.1.1

and I am using Xcode 5.1.1

推荐答案

这就是我最终得到的结果。结合 @Konrad Rudolph的答案和@Joachim Pileborg的评论

this is what I have ended up with. combined with @Konrad Rudolph's answer and @Joachim Pileborg's comment

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

更多推荐

找出汽车的类型

本文发布于:2023-11-03 07:03:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554517.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   汽车

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!