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 7663669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:06:27+00:00 2026-05-31T14:06:27+00:00

I am writing a code which uses templates to support int datatype and char*

  • 0

I am writing a code which uses templates to support int datatype and char* datatype. lets say

struct node {
    KeyType key;
    struct node *next;
};

And this is sorted linked list node, so whenever I insert node, I have to compare. For this I created one header file comparison.h, in which I have defined comparison operation like

LT(a,b)..
GT(a,b)..

And when I use node<int> I define LT(a, b) as a<b and in case of node<char*> strncmp(a, b, SIZE) ( Using macro I switch the definition )
So Is there any way by which I minimally interfere this comparison.h. What should be the better ways of using comparison or specific operations specific to datatypes when using templates ?

  • 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-05-31T14:06:28+00:00Added an answer on May 31, 2026 at 2:06 pm

    Use template specialization. For example:

    #include <iostream>
    #include <string>
    using namespace std;
    
    template <class T>
    class node {
        T data;
      public:
        node(){}
        static bool GT (const T& first, const T& second);
    };
    
    template <class T>
    bool node<T>::GT(const T &first, const T &second)
    {
        std::cout << "general use" << std::endl;
        return first > second;
    }
    
    template <>
    bool node<string>::GT(const string &first, const string &second)
    {
        std::cout << "called from a string" << std::endl;
        return first.compare(second)>0;
    }
    
    int main () {
      std::cout << node<int>::GT(10,20) << std::endl;
      std::cout << node<string>::GT("Hello","World") << std::endl;
      return 0;
    }
    

    The output will be:

    general use
    0
    called from a string
    0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a compiler which uses C as an intermediate code which is (Currently)
I'm writing a select query in a C# method which lets client code provide
I'm writing a few unit tests of some code which uses sys.stderr.write to report
I am writing a simple HTML code which uses YUI autocomplete (to display suggestion
I'm writing some Scala code which uses the Apache POI API. I would like
I'm writing unit tests for code which uses JDOM to read and write XML.
I'm writing a Groovy script which uses third party java code that I can't
I'm writing an API which uses MongoDB as the storage backend. Let's say the
I'm writing some code which could really do with some simple compile time metaprogramming.
I am writing some code in which i need to get a line from

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.