-1

Problem: overload the default << operator for a vector<int> v variable, so that cout << v will print each element sequentially. Here I saw one option as proposed by Jason Iverson in the previous question

template<typename T>
std::ostream& operator<<(std::ostream& s, std::vector<T> t) { 
    s << "[";
    for (std::size_t i = 0; i < t.size(); i++) {
        s << t[i] << (i == t.size() - 1 ? "" : ",");
    }
    return s << "]" << std::endl;
}

Obviously this should work for any type of elements, however since I am only concerned with int, I simplified the function as

ostream& operator << (ostream &os, const vector<int> &v){
    for (auto x: v){
        os << " " << x;
    }
    return os;
}

It just works fine.

So my question is

  1. As far as vector<int> v is concerned, what is the caveat of not using template?
  2. Apart from being general-purpose, what is the advantage of Jason's solution?

Thanks!


Deeper thoughts: The above question was poorly phrased, and thanks to the comments, I believe it makes sense to rephrase it. Question: in C++, what is the cost of using template where a single-type function is enough?


Credits to @Marco A and @Walter, this question could be closed.

Community
  • 1
  • 1
wenduowang
  • 37
  • 9
  • 4
    Do you know what a `template` is? If so, why are you asking, if not, go and learn about it. – Walter Jan 20 '17 at 15:38
  • What over advantage then general purpose/not having to write the overloads yourself do you need? – NathanOliver Jan 20 '17 at 15:39
  • @Walter I know `template` but want to understand its advantage, other than being more general by not specifying the type `T`. – wenduowang Jan 20 '17 at 15:50
  • Overloads != function templates. With a function template you have one piece of code for a huge set of types, with overloads you'll have to define each one of them. Plus SFINAE. Plus template/function differences in the compilation process. It's like comparing apples to oranges. – Marco A. Jan 20 '17 at 15:50
  • @NathanOliver Here I am only concerned with the integer type vector, so I think it suffices to overload the operator for just this type should be ok. On the other hand, does defining a template bring extra overhead to the program. One reason that I am asking is I seldomly see codes overloading the default operator outside a class. – wenduowang Jan 20 '17 at 15:54
  • Who is Jason Iverson? What "previous question"? Do you want to link to whatever you're talking about? Is he even relevant to the question? – Barry Jan 20 '17 at 15:54
  • @MarcoA. Thanks for insights of SFINAE! – wenduowang Jan 20 '17 at 16:23
  • @Barry So sorry for forgetting providing the link earlier. – wenduowang Jan 20 '17 at 16:24

1 Answers1

0

One of the main points of C++ is generic programming and templates are the way to do this.

The advantage is pretty obvious: you don't have to write the same/similar piece of code more than once and you don't have to debug/maintain similar pieces of code, but just one. (All of this falls in your category "general purpose", so there is no advantage beyond that).

There is actually some disadvantage, because a template is not a function (or class). A function (or class) will only be created from the template at compile time when it is actually used, whereas your (non-template) function may be pre-compiled. This has two implications: (1) more compiling and (2) that certain syntax errors in the code only turn up when the template is used with arguments for which they won't work. The template in your post, for example, won't compile if ostream << T has not been defined.

Walter
  • 44,150
  • 20
  • 113
  • 196
  • Is there any downside of having such versatility, or does it come at the same cost as defining a narrow-scoped function? – wenduowang Jan 20 '17 at 16:35