Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7810069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:38:38+00:00 2026-06-02T03:38:38+00:00

Is it possible to do something like this: template <typename T, typename Comparator =

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:38:39+00:00Added an answer on June 2, 2026 at 3:38 am

    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> for std::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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something like this in my code: template <typename T> struct A {
I'd like to build something like this: File 1: template<typename Vector> namespace myNamespace {
Is there any way to make something like this work: template<typename T, typename X,
Ok, so I have something setup basically like this: template<typename T> void example() {
Is it possible to do something like this : <property name=template value=file:/some/resource/path/myTemplate.txt /> via
Having trouble inheriting from a template class. Looks something like this: template<typename type> class
I need a template like this, which work perfectly template <typename container> void mySuperTempalte
Basically, what I want to do is something like this: template<typename x,typename y,typename z>
Is it possible to do something like this? public function something() { $thisMethodName =
is it possible to do something like this in JSTL: <div class=firstclass<c:if test='true'> someclass</c:if>>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.