C ++ 0x auto关键字的含义,通过示例? [英] Meaning of C++0x auto keyword, by example?
问题描述
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
isFoo<T>*
, but forb
,auto
isFoo<T>
?
是的。
这篇关于C ++ 0x auto关键字的含义,通过示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!