RE: Is this error correct
It appears that the problem is related to the default template argument
for vector because the following works fine:
#include <vector>
template <template <typename T, typename U> class X, typename T, typename U>
void f(X<T, U>) { }
int
main() {
std::vector<int> v;
f(v);
}
It seems clear that std::vector should be usable wherever a class
requiring one template argument is expected, so in the original example,
I believe the behavior of g++ is correct, while MSVC's behavior is a bug.
"mps" wrote:
The following program compiles cleanly under g++ but fails to deduce template
arguments in VS2005. It seems like it should be able to do so. Is this a bug?
// foo.cpp : Defines the entry point for the console application.
//
#include <vector>
#include <iostream>
using std::vector;
template <template<typename> class Container, typename T>
T median(const Container<T>& c) {
using std::back_inserter;
using std::copy;
using std::vector;
vector<T> vec(c.begin(),c.end());
typedef typename vector<T>::size_type vec_sz;
vec_sz size = vec.size();
sort(vec.begin(),vec.end());
vec_sz mid = size/2;
return size % 2 == 0
? (vec[mid] + vec[mid-1]) / 2
: vec[mid];
}
int
main() {
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
std::cout << median(v);
return 0;
}