Solved

Templated template parameters

Posted on 2009-05-07
2
229 Views
Last Modified: 2012-05-06
It is my understanding that the C++ standard supports templated template parameters, where a template parameter itself is templated.  Recently, I recompiled some old code after upgrading to a higher version of g++.  It was complaining about a templated function where the template parameter was templated.  Since this code had always worked before, and has been working fine for the past couple of years, I was confused why this new version of g++ was having a problem with it.  So, I came up with a very simplified test case, shown below.  This new version of g++ (4.3.2) will not compile the code below, and yet as far as I can see it's perfectly legitimate and works fine when compiled using g++ 4.1.2.

On the other hand, lately newer versions of g++ have only been getting more strict when it comes to the standard, so I wonder if I am misunderstanding the use of templated template parameters, and the code below is in fact not legal for some reason.  

So, my question is, is the code below legal or is this new version of g++ incorrect?  And if g++ is correct about this, how can I fix the code below?
template <template <class> class Array>
void dosomething(const Array<std::string>& array)
{
 
}
 
int main()
{
	std::vector<std::string> vec;
	dosomething(vec);
}

Open in new window

0
Comment
Question by:chsalvia
  • 2
2 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 24324952
>> It is my understanding that the C++ standard supports templated template parameters
Correct


>> So, my question is, is the code below legal or is this new version of g++ incorrect?
The code is incorrect. You MUST match all the template paramters of vector. Since vector takes 2 params (type and allocator) your template template param must match this too.

The code below built fone for me on both VS2008 and g++ 4.2.4
#include <string>
#include <vector>
 
template <template <typename _Ty, typename _Ax> class Array>
void dosomething(const Array<std::string, std::allocator<std::string> >& array)
{
 
}
 
int main()
{
	std::vector<std::string> vec;
	dosomething(vec);
}
 

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 24324967
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question