C ++ 0x auto关键字的含义,通过示例? [英] Meaning of C++0x auto keyword, by example?

查看:237
本文介绍了C ++ 0x auto关键字的含义,通过示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

auto a = (Foo<T>*)malloc(sizeof(Foo<T>));
auto *b = (Foo<T>*)malloc(sizeof(Foo<T>));

我认为模板不重要,但问题是: a b 是同一类型吗?

I don't think it's important that templates are there, but the question is: are a and b of the same type?

g ++ -std = c ++ 0x -Wall (4.4)不给出任何错误或警告,但我没有运行程序,所以我不知道它是否一样

g++ -std=c++0x -Wall (4.4) doesn't give any errors or warnings, but I haven't run the program so I don't know if it does the same thing.

这是否意味着 a auto Foo * ,但 b auto Foo< T>

Does this mean that for a, auto is Foo<T>*, but for b, auto is Foo<T>?

推荐答案


是同一类型的 a b

让我们来看看,我们会不会?

Let's find out, shall we?

#include <cstdlib>
#include <type_traits>

template <typename T>
struct Foo
{
    T member;
};

template <typename T>
void test()
{
    auto  a = (Foo<T>*)malloc(sizeof(Foo<T>));
    auto *b = (Foo<T>*)malloc(sizeof(Foo<T>));

    static_assert(std::is_same<decltype(a), decltype(b)>::value, "same type");
}

template void test<int>();   // explicit instantiation

这样编译时没有静态断言失败。

This compiles without a static assertion failure.


这是否意味着 a auto 是<$但是对于 b auto Foo * code> Foo

Does this mean that for a, auto is Foo<T>*, but for b, auto is Foo<T>?

是的。

这篇关于C ++ 0x auto关键字的含义,通过示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆