2

While designing a class or a function, which way, that is shown below, is better and why?

class Container {

    //Provide this functionality??
    void addItemVariadic(const Value& val, ...);

    //Or provide this functionality??
    void addItemList(const list<Value>& vals);

};

Is it better to provide a function like addItemVariadic(..), or addItemList(..)?

Or is it better to provide a set of such functions, like further taking some iterators, or is it better to limit the functionality, like just taking a list?

phoad
  • 1,801
  • 2
  • 20
  • 31
  • There are more options - you could write a function that takes iterators as input, that way it will work with all the stl containers. The syntax looks roughly like this: template void foo(InputIterator begin, InputIterator end) – aCuria Feb 15 '13 at 15:43

1 Answers1

2

Using variadic functions is dangerous

If you ever pass a variable which has not the appriopriate type, it will crash at run time, when calling the function.

On the contrary if you use a std::list, it won't compile simply, and you avoid a crash.

Btw: I advice you to use std::vector instead of std::list in this case.

Edit1 Possible Dupplicate Question. with a nice solution using operator << to inputs all the parameters in one shot.

Edit2 So as to choose between different std::containers, there is a choose-graph as answer to this question. This graph addresses at C++03, it doesn't cover the new containers introduced in C++1.

Community
  • 1
  • 1
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
  • Is it better to add the items just one by one, or providing functionality of adding them in one shoot? Why using std::vector may be beneficial, because of less overhead of pointers? – phoad Jan 18 '13 at 12:36
  • the rule is that std::vector does the job of containers 95-99% of the time. We should use std::deque, std::list, std::forward_list, only if we know why: I'm gonna link you to a graph helping to choose that. Adding your variables all in one shoot needs a variadic function ;-). – Stephane Rolland Jan 18 '13 at 12:40
  • I mean calling the addItem() once per item, or calling it with providing a vector to it. – phoad Jan 18 '13 at 12:43
  • 1
    @phoad If you're calling it providing the vector, you are asking the outside world of your business class/function to do the work of constructing the vector and adding elements. On the contrary, if you use addItem this work is delegetated to the internal of addItem. It depends on the number of time in your code your function is called. If it is several times, then you have all the boiler plate code of vector construction spread everywhere. ( just to give you the hint ). it's your choice after and your taste :-) – Stephane Rolland Jan 18 '13 at 12:46
  • @phoad I've edited my answer and gave you a link to someone who asked quite the same question. There's a nice solution provided there, **using operator <<** to put all the inputs in one shot. Probably easier to use than the std::vector solution. – Stephane Rolland Jan 18 '13 at 12:50
  • 1
    @phoad and in Edit2 a link to the question were there's a graph for choosing between containers. – Stephane Rolland Jan 18 '13 at 12:54