why i can't use auto on typedef?

why i can't use the 'auto' on typedef?
What do you mean? Like this?
 
typedef auto foo;

What would that do, if it was possible? Would it just make 'foo' a synonym for 'auto'? Why would that be useful?
I would think that typedef requires that the type is resolved at compile time.
i mean use 'foo' (for example), instead 'auto'.
because, in same cases, you know what type returns, but you must use 'auto' for work... that's why i need a synonym
You never "have" to use auto, you can specify the correct type instead.

just for test can i get the 'auto' type?
You're not making any sense. You say you want to be able to write
 
foo x = f();
but if 'foo' is a synonym of 'auto' then you should also be able to write
 
auto x = f();
so what's the problem? Why don't you just use auto?

This reminds me of some silly defines I read about once:
1
2
3
#define whilst while
#define when if
#define otherwise else 


EDIT: I think I know what you want.
1
2
auto x = f();
decltype(x) y; //y is of the same type as x, regardless of what that is 
Last edited on
auto requires context in order to denote a type. When the compiler sees typedef there is no context and hence no type. Therefore you cannot use typedef in conjunction with auto.

While technically it would be possible that typedef covers auto, but obviously the developer of the language do not see any use for it.
i losed the last reply(too much time i was log off) :(
1
2
auto vec2....
std::cout << typeid(vec2).name() << '\n';

output:
42
St6vectorIS_IS_IS_IiSaIiEESaIS1_EESaIS3_EESaIS5_EE
yah.. too big lol
now you hunderstand why i was trying using the 'auto' on 'typedef' lol
everyone severely avoid use macro\preprocessor code, but in these case is great ;)
#define VectorMulti auto
ok... it's an opinion, but i'm happy with it hehehe
if i can't use the 'auto' on 'typedef', i must live with it and use another option.

PS: i'm trying convert "var hello[7][8][10] as integer={0}"
to vector 3D.. but it's too complex... but using the code from last topic, it's more easy

thank you so much for all to all
output:
42
St6vectorIS_IS_IS_IiSaIiEESaIS1_EESaIS3_EESaIS5_EE
yah.. too big lol
now you hunderstand why i was trying using the 'auto' on 'typedef' lol
Nope. Not really. The value of typeid(vec2).name() is implementation-defined and a run-time value. I don't see at all what it has to do with this discussion.

everyone severely avoid use macro\preprocessor code, but in these case is great ;)
#define VectorMulti auto
That's so stupid. Do you realize that it doesn't accomplish anything?
1
2
VectorMulti vec2 = VM<int>() << 2 << 3 << 4 << 5 << complete(42);
VectorMulti vec3 = (std::string)"this is retarded";
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
template <typename T>
struct CompleteVector{
    T value;
    CompleteVector(const T &value = {}) : value(value){}
    CompleteVector(T &&value) : value(std::move(value)){}
    CompleteVector(const CompleteVector &) = default;
};

template <typename T>
CompleteVector<T> complete(const T &x){
    return CompleteVector<T>(x);
}

template <typename T>
struct VectorMultiArray{
    std::vector<size_t> dimensions;
    VectorMultiArray() = default;
    template <typename T2>
    VectorMultiArray(const VectorMultiArray<T2> &x) : dimensions(x.dimensions){}
    template <typename T2>
    VectorMultiArray(VectorMultiArray<T2> &&x){
        this->dimensions = std::move(x.dimensions);
    }
    VectorMultiArray<std::vector<T>> operator<<(size_t n) const{
        VectorMultiArray<std::vector<T>> copy = *this;
        copy.dimensions.push_back(n);
        return copy;
    }
    template <typename T2>
    T operator<<(const CompleteVector<T2> &n) const{
        T ret;
        initialize(ret, n.value, &this->dimensions[0], this->dimensions.size());
        return ret;
    }
private:
    template <typename V, typename T2>
    static void initialize(V &dst, const T2 &value, const size_t *sizes, size_t n){
        dst.resize(sizes[0]);
        for (auto &i : dst)
            initialize(i, value, sizes + 1, n - 1);
    }
    template <typename T2>
    static void initialize(std::vector<T2> &dst, const T2 &value, const size_t *sizes, size_t n){
        dst.resize(sizes[0], value);
    }
};

template <typename T>
std::vector<std::vector<T>> operator<<(const std::vector<T> &x, size_t n){
    return std::vector<std::vector<T>>(n, x);
}

i had tested
helios: "That's so stupid. Do you realize that it doesn't accomplish anything?"
you have right!!! the 'VectorMulti' it's a word that is changed by 'auto'... nothing more... but if all var name must be a valid name that we do, so why not use the macro for be more simple too?
How is "VectorMulti vec2" simpler than "auto vec2"? I would argue it's less clear, because someone unfamiliar with the code might think that "VectorMulti" is a real type, and might try to do something like
 
auto p = std::make_unique<VectorMulti>();
ok... maybe the word isn't the best.. but you get my point ;)
maybe you can give me a better word for i use... it's a multidimensional vector
maybe you can give me a better word for i use.
I think "auto" works just fine.
ok.. i will accept all you said.
thank you so much for all
If the problem is that you cannot see that vec2 is a "multidimensional vector" then you might want choose a more descriptive variable name instead.

 
auto multivec2 = ...;

Even comments would be better than fake type names.

 
auto vec2 = ...; // multidimensional vector 
Peter87 that's a good point too. thank you
Topic archived. No new replies allowed.