1
#include <vector>
#include <iostream>
#include <string>
using namespace std;

#include <vector>
#include <iostream>
#include <string>
using namespace std;
// Template-template argument must
// be a class; cannot use typename:
template<typename T, template<typename> class C>
void print2(C<T>& c) {
  copy(c.begin(), c.end(),
       ostream_iterator<T>(cout, " "));
  cout << endl;
}
int main() {
  vector<string> v(5, "Yow!");
  print2(v);
} ///:~

This code looks perfect right to me. But this snippet can't compile in My Mac. The error information is the following

 note: candidate template ignored: substitution failure [with T = std::__1::basic_string<char>]: template template argument has
      different template parameters than its corresponding template template parameter

   void print2(C<T>& c) {
         ^
    1 error generated.
SergeyA
  • 61,605
  • 5
  • 78
  • 137
zhexuany
  • 100
  • 6

1 Answers1

4

This is because std::vector is not a single-argument template. Standard mandates an element type and allocator type arguments for std::vector.

If you are not on legacy C++ and can use variadic templates, you can declare your function like this:

template<typename T, template<typename...> class C>
void print2(C<T>& c);
SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • This code is copied from thinking in c++. It does compile in gcc with version 3.3.1. I guess vector's definition has changed a lot since then. – zhexuany May 12 '16 at 20:42
  • @zhexuany, please do yourself a favor and get rid of this ice age artifact. – SergeyA May 12 '16 at 20:43
  • 1
    "your implementation might have even more than that" - no, it's exactly two. Anything else is non-conforming. – T.C. May 12 '16 at 20:46
  • @SergeyA: I'm curious to know which one is the "ice age artefact", the book or the compiler... :-D The book is supposedly good, or at least it's in the [list of recommended C++ manuals](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), but it's really old, and I would never recommend it. – Fabio says Reinstate Monica May 12 '16 at 20:50
  • @T.C., I didn't find explicit wording in standard which allows it in 'Conforming Implementations', but I didn't find anything which would disallow it either. I would remove it from my answer. – SergeyA May 12 '16 at 20:54
  • 1
    @FabioTurati Me am think compiler. I go now. Invent fire. – user4581301 May 12 '16 at 20:55
  • @FabioTurati, never read the book. I was talking about gcc 3. – SergeyA May 12 '16 at 20:55
  • @SergeyA I just installed the compiler for verifying the code can compiled in gcc 3. I already deleted it. I am totally beginner for template programming. Could you recommend me some books about template programming? – zhexuany May 12 '16 at 20:59
  • @zhexuany, I am a firm believer in "Programming: Principles and Practice Using C++" From Bjarne Stroustrup. It somehow got a reputation of 'not being for beginners', but I actually think it is the **best** book for beginners. I used it to learn C++ long time ago, and I never regretted. You can follow the list given ny Fabio above. – SergeyA May 12 '16 at 21:01
  • @SergeyA I will definitely check that book out. I am using thinking in c++ right now. Pretty old. Some snippet can't compile. I have one year experience in using c++. For the summer break, I want to learn new stuff such as c++ template programming. ThX!!!! – zhexuany May 12 '16 at 21:13