Reputation: 43
I'm having some difficulty wrapping my head around this one, likely because I don't fully understand the underlying mechanics.
I have a struct like the following:
template <class T>
struct A {
int id;
T x;
}
This struct is then used as a type for a shared_ptr:
typedef std::shared_ptr<A> A_ptr;
Which in turn gets stored in a map:
typedef unordered_map<int , A_ptr> AMap;
However, when I compile, I get the following error:
> error: type/value mismatch at argument 1 in template parameter list
> for 'template<class _Tp> class std::shared_ptr' typedef
> std::shared_ptr<A> A_ptr;
I have read other posts about a similar issue, but all of them recommend creating the shared_ptr A inside the struct. I've tried this, even though I can't logically understand why it would help with my problem, but it doesn't work. Any help is appreciated.
Upvotes: 0
Views: 49
Reputation: 170074
A
is the name of a template, not the name of a class. If you were to name a specialization, your typedef
would be fine:
typedef std::shared_ptr<A<int>> A_int_ptr; // A<int> **is** a class
If you want to keep the code generic, you'll need to switch to using alias templates instead of regular type aliases:
template<typename T>
using A_ptr = std::shared_ptr<A<T>>;
template<typename T>
using AMap = std::unordered_map<int , A_ptr<T>>;
Now everything is properly templated, and the instantiations depend on the type T
you intend to pass onto A
.
Upvotes: 8