How come I can't instantiate an object of type Foo with above constructor?
I have a class Bar that uses an internal typedef (as a workaround for "template typedefs") and intend to use it in a constructor as below (CASE 1). However, I don't seem to get it to compile. Is this legal C++? CASE 2 seems to suggest the problem is related to the typedef in Bar.
How can I define a constructor that will accept std::vectors of objects with the type in Bar?
#include <vector>
#include <iostream>
#include <utility>
template <typename T>
struct Bar
{
typedef std::pair<T, T> type; // or anything else that uses T
};
struct Foo
{
Foo() {}
// CASE 1: doesn't compile
template <typename T> explicit Foo( const std::vector<typename Bar<T>::type>& data )
{
std::cout << "Hello\n";
}
//// CASE 2: compiles, but it's not what I want
//template <typename T> explicit Foo( const std::vector<Bar<T> >& data )
//{
// std::cout << "Hello\n";
//}
};
int main()
{
std::vector<Bar<int>::type> v; // for CASE 1
//std::vector<Bar<int> > v; // for CASE 2
Foo f( v );
return 0;
}