find out the type of auto

2019-01-15 11:50发布

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?

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

example:

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);

output

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
*

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

and I am using Xcode 5.1.1

3条回答
beautiful°
2楼-- · 2019-01-15 12:15

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

Well this is easy.

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

just do a is_lvalue<decltype(x)>::value and is_value<decltype(x)>::value.

An rvalue (temporary or moved reference) is a non-literal non-lvalue.

查看更多
虎瘦雄心在
3楼-- · 2019-01-15 12:29

Use GCC’s __cxa_demangle function:

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';
};
查看更多
劳资没心,怎么记你
4楼-- · 2019-01-15 12:30

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);

}

and output

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
查看更多
登录 后发表回答