Thread: End of a variable argument list

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tabstop
    The C++ way to do it, as also mentioned above, is to use a std::vector, which is an object that can be resized at will and that does know start and end.
    I would not say that that is the C++ way to do it, considering that std::max_element makes use of a range denoted by an iterator pair, rather than a vector.

    Quote Originally Posted by manasij7479
    Here is an example...of that function..
    Yuck
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by laserlight
    Yuck
    Why ? ...Is iterating better than popping out the last each time ?

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it is.
    (Why?)
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    Yes, it is.
    (Why?)
    I just realised that just iterating does the same thing..just without the popping... so it'd be better..

    @laserlight : Any other yucky points ?
    Code:
    template<class T>
    T my_max(std::vector<T> v)
    {
        T temp=v[0];
        for(auto x:v)
            if(x>temp)
                temp=x;
        return temp;
    }

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The "class" keyword is used for backward compatibility. The new keyword for use in templates is really "typename".
    Consider passing the vector by (const) reference.
    Ideally, I'd use std::initializer_list instead of std::vector.
    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
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    It does not know how many arguments were passed nor does it care. That's why it should be avoided.
    No, it should be avoided because it does not enforce type safety.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK yes, you're right. I see my point got across incorrectly.
    My point was that it was one of the reasons to avoid it, and not the only or the reason. There are many reasons to avoid 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.

  8. #23
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    why not use variadic templates? it really seems like it would be the C++ way to do exactly this.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Variadic templates allow for a infinite amount of template arguments or types. So if it isn't types, then it must be static arguments (known at compile time).
    For runtime arguments, std::initializer_list is exactly what was created for that purpose.
    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.

  10. #25
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elkvis View Post
    why not use variadic templates? it really seems like it would be the C++ way to do exactly this.
    Template arguments and function arguments aren't the same thing, though they could be used in this case if the number list is a compile time constant.

  11. #26
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    Variadic templates allow for a infinite amount of template arguments or types. So if it isn't types, then it must be static arguments (known at compile time).
    For runtime arguments, std::initializer_list is exactly what was created for that purpose.
    I'll agree that std::initializer_list is an excellent way to do this; however, there's no reason why you couldn't write the code (and its accompanying documentation) such that the function expects only int parameters, and assumes internally that they are integer values. you could then recursively run through the list and add each parameter to a vector, from which you could then determine the largest value, or whatever you like. from the perspective of the user of the function in question, the variadic template method might actually be a little quicker and more intuitive.

    with regard to the initializer_list method, is there a reason why you can't make the function accept a vector and pass it an initializer list? I've tested the following code with g++ 4.6, and it compiles perfectly:
    Code:
    int foo(std::vector<int> vInt)
    {
      int largest = vInt.front();
      for (int i : vInt)
      {
        if (i > largest) largest = i;
      }
      return largest;
    }
    
    int bar()
    {
      return foo({1, 2, 3, 4, 5, 6, 7, 8, 9, 0});
    }

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    I'll agree that std::initializer_list is an excellent way to do this; however, there's no reason why you couldn't write the code (and its accompanying documentation) such that the function expects only int parameters, and assumes internally that they are integer values. you could then recursively run through the list and add each parameter to a vector, from which you could then determine the largest value, or whatever you like.
    There is nothing stopping you from doing that, except perhaps some extra overhead.
    But then again, with an initializer list, there's no point in doing so.
    It's good for C++03 code, though.

    from the perspective of the user of the function in question, the variadic template method might actually be a little quicker and more intuitive.
    This isn't a variadic template method, and it wouldn't work properly.

    with regard to the initializer_list method, is there a reason why you can't make the function accept a vector and pass it an initializer list? I've tested the following code with g++ 4.6, and it compiles perfectly:
    You are contradicting yourself. What is the question?
    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.

  13. #28
    Registered User GigaRoid's Avatar
    Join Date
    Aug 2011
    Location
    Everywhere
    Posts
    23
    Quote Originally Posted by Elysia
    There is no empty slot.
    va_arg is dumb; it simply fetches a value adjacent in memory and increments that position each time you call it.
    It does not know how many arguments were passed nor does it care. That's why it should be avoided.
    I'm not sure if you're understanding... we have 5 arguments: smax(5,1,4,8,6). va_arg will keep going on forever, creating an infinite loop, so we have to stop it at the sixth argument. So either it returns some an empty value at six, or it returns 0, which won't help us at all. If it returns an empty value, we can check for that. If that doesn't work... is there any way for us to be able to find the number of arguments without user input?

    EDIT: didn't see the second page there.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by GigaRoid View Post
    So either it returns some an empty value at six, or it returns 0, which won't help us at all. If it returns an empty value, we can check for that. If that doesn't work... is there any way for us to be able to find the number of arguments without user input?
    va_arg does neither. It simply returns whatever is written at that memory location. That is, if we can read that. If not, we'll likely get an access violation. In either case, it's undefined behavior.
    Simply put, it's not possible without manual input to do this with va_arg, and this is because of its C nature. C does not hold your hand.
    The C++ alternatives are more friendly, and that is why I recommend you look into those.
    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.

  15. #30
    Registered User GigaRoid's Avatar
    Join Date
    Aug 2011
    Location
    Everywhere
    Posts
    23
    Okay, so since everyone is arguing, I just blindly choose one of them?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable Argument List
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-16-2007, 05:35 AM
  2. Passing Through A Variable Argument List
    By SMurf in forum C Programming
    Replies: 6
    Last Post: 04-14-2007, 11:12 AM
  3. Variable Argument List Passing
    By Orborde in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 08:42 PM
  4. I'll have a Variable argument list to go...
    By SMurf in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 02:02 PM
  5. variable-length argument list ???
    By null in forum C Programming
    Replies: 5
    Last Post: 10-14-2001, 03:18 PM