找出汽车的类型

find out the type of auto

本文关键字:类型 汽车      更新时间:2023-10-16

我在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(临时引用或移动引用)是一个非文字的非左值。