Is it possible to do something like this:
template <typename T, typename Comparator = std::less<typename T::Key>>
struct Container
{
std::map<typename T::Key, T*, Comparator> m;
};
This is the problem part: typename Comparator = std::less<typename T::Key>
i.e. have a typedef within the implementation of the T be used for the specialization of the comparator. Is it always necessary that the thing that the comparator uses (T::Key) be specified as an explicit parameter to the template?
I realize there are other ways around this by changing the T class, (e.g. require an explicit T::compareKey method), but the question is more around whether template syntax can be used to achieve something like this.
I’m sure that’s valid, as long as all the necessary definitions are available when the template is instantiated, and it certainly works for me.
Are you including
<map>? It’s possible that you might also need to include<functional>forstd::less, although I’m sure that must be included by<map>.Does your compiler support C++11, and have you enabled that support? Older compilers (or those configured for strict C++03 compliance) will get confused by your use of
>>to close two template parameter lists; they will interpret it as the right-shift operator. In that case, you can fix the problem by adding a space between them.If neither of these are the problem, please include a minimal, complete example that demonstrates the problem, the exact error message with an indication of which code lines it refers to, and let us know which compiler you’re using.