C ++ 0x auto keyword value, for example?

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

I do not think it's important that the patterns are, but the question is, aand bof the same type?

g++ -std=c++0x -Wall (4.4) does not give any errors or warnings, but I did not run the program, so I do not know if it does the same.

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

+3
source share
2 answers

They are a, and bof the same type?

Find out if we will?

#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 is a compilation without a static statement.

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

Yes.

+12

auto . a b . , auto , Foo<T>.

, , :

template<class U>
void func_a(U);

template<class U>
void func_b(U*);

template<class T> struct Foo {};

template<class T>
void test() {
   func_a( new Foo<T> ); // U = Foo<T>*
   func_b( new Foo<T> ); // U = Foo<T>
}

auto , U .

, , - :

void test2() {
    auto x = {1,2,3};  // OK, decltype(x) --> initializer_list<int>
    func_a( {1,2,3} ); // Illegal, U cannot be deduced because
                       // {1,2,3} is not considered an expression
}

.

+2

Source: https://habr.com/ru/post/1766337/


All Articles