CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2007
    Posts
    35

    template template parameter issue

    I am wondering what's wrong with template parameter in print()?
    I was tring to use print without specifying template argument.


    This is the error I got.
    error C2784: 'void print(const C<T> &)' : could not deduce template argument for 'const C<T> &' from 'std::vector<_Ty>'


    Code:
    #include <vector>
    #include<iostream>
    #include <iterator>
    using namespace std;
    
    template<typename T, template<class > class C >
    void print(const C<T> &x) 
    {
    	copy(x.begin(), x.end(), ostream_iterator<T>(cout," "));
    	cout << endl;
    }
    
    int main()
    {
       vector<double> v;
       v.push_back(100);
       print(v);
    
    }

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: template template parameter issue

    For one thing std::vector takes two template arguments (contained type and allocator), so it doesn't match the function argument.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Oct 2007
    Posts
    35

    Re: template template parameter issue

    Thanks Graham,

    Code:
    //correct defition
    template<typename T, template<class U, class Allocator = allocator<U> > class C>
    void print(const C<T> &x) 
    {
    	copy(x.begin(), x.end(), ostream_iterator<T>(cout," "));
    	cout << endl;
    }
    Sam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured