I just don't get this...

This is a discussion on I just don't get this... within the C++ Programming forums, part of the General Programming Boards category; Code: for (auto i = vec.begin(); i != vec.end(); ++i) Or I wonder if this works... Code: for (auto i ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Code:
    for (auto i = vec.begin(); i != vec.end(); ++i)
    Or I wonder if this works...
    Code:
    for (auto i = vec.begin(), end = vec.end(); i != end; ++i)
    Or your example, of course...
    Code:
    for (auto end = vec.size(), i = 0; i != end; ++i)
    Shortcuts. Gotta love 'em.
    Hopefully they DO work.
    Iterator method works for sure, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Ah! That new C looks too simple o.o That can never be good...
    Currently research OpenGL

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    C++, and you gotta see it to believe it. It's going to make things so much easier for programmers. You will see. You will welcome it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    So was the nuclear reaction :P
    Anyways, if it makes it C++ simpler, must it then not make it... Not as good? Like, slower, less features, something?
    Currently research OpenGL

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Nope. It will actually make it more flexible, because with template programming, functions can return some incredibly complex type (like boost::bind or stuff). So having auto makes it simpler while allowing for more, flexible and complex functionality!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    It sounds good, but you only tell the good sides :P there are always bad sides too
    I'm a bit sceptical(?) about this new C++...
    Currently research OpenGL

  7. #22
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In particular, why should the auto keyword (automatic deduction of variable type from the return type of a function) cause any runtime penalty - it only happens at compile-time. So, it can only make compiling slower (and probably not much here). It's trivial for the compiler to know what type vec.begin() returns, whereas it is not as simple for the programmer - you could easily type std::vector<X>::iterator where it actually should be std::vector<X>::const_iterator and get an "incomprehensible" compiler error.

    There are also features that are supposed to make C++ programs faster (e.g "move constructors").
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Well, it is possible to abuse auto...
    Code:
    auto i = 100; // i = ???
    But that's the story with everything. No, the new standard will bring more good than bad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Quote Originally Posted by anon View Post
    I wonder if C++0x would allow this though
    Code:
    for (auto end = vec.size(), i = 0; i != end; ++i)
    Hmm... no it wouldn't. You still need a type declaration for i. However, you will be able to do this:

    Code:
    for (auto end = vec.size(), decltype(end) i = 0; i != end; ++i)

    Quote Originally Posted by Elysia
    Well, it is possible to abuse auto...
    Code:

    Code:
    auto i = 100; // i = ???
    IIRC that will in fact produce a compile time error. auto usage cannot result in ambiguity.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #25
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The Wikipedia article however uses this as an example. Don't the literals in their various forms have a default type that they are associated with, for which reason i should be int without ambiguity?

    As to the decltype example, I don't think you can do
    Code:
    for (unsigned i, unsigned end; ...; ...)
    and therefore your example probably wouldn't work either.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #26
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    A little more about struct's :P
    "4-6. Rewrite the Student_info structure to calculate the grades immediately and store only the
    final grade."
    I tried making struct like this:
    Code:
    struct Student_info{
        double grade(double, double);
    
    };
    But that gave me "17|undefined reference to `Student_info::grade(double, double)'|" error, any advice? :P
    Currently research OpenGL

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You do have to eventually define the function somewhere, I would guess.

    Edit: And since it's inside a struct, you can't just use the name, you have to indicate that you mean the function inside the struct. So you'd have to define it just like your error message says: Student_info::grade(double this, double that).
    Last edited by tabstop; 11-17-2008 at 04:54 PM.

  13. #28
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    ok, thanks ^^
    Currently research OpenGL

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21