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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:21:21+00:00 2026-05-16T11:21:21+00:00

Consider something like… template<typename T> class Vector { … bool operator==( const Vector<float> &rhs

  • 0

Consider something like…

template<typename T>
class Vector {
  ...
  bool operator==( const Vector<float> &rhs ) {
    // compare and return
  }

  bool operator==( const Vector<T> &rhs ) {
    // compare and return
  }
  ...
 };

Notice how the specialization is above the non specialized version. If I were to put the specialized version below the non-specialized version, would a Vector<float> == comparison still work as intended? For some reason I think I remember reading that if you put the specialization below in this scenario then when the compiler looks through the header, it will see the default first, see that it works, and use that.

  • 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-16T11:21:21+00:00Added an answer on May 16, 2026 at 11:21 am

    As written, I believe you have an error. You aren’t specializing operator==, you’re overloading it. You aren’t allowed to overload on a template parameter like that.

    If your method were a free function, then you could specialize it. For instance

    template <typename T>
    void Foo(Vector<T> x) {
       std::cout << "x" << std::endl;
    }
    template <>
    void Foo(Vector<float> y) {
       std::cout << "y" << std::endl;
    }
    
    ...
    Vector<int> intVec;
    Vector<float> floatVec;
    Foo(intVec); //prints x
    Foo(floatVec); //prints y
    

    In this case, if you call Foo with Vector<float>, you’ll call the specialized version.

    Order matters to some degree. First of all, the compiler will always favor a specialization over an unspecialized form when one is available. After that, the compiler will try to instantiate each function in order. If it can compile it successfully, that specialization will be used. If it can’t, it will move on to the next one. This principle s called Substitution Failure is not an Error (SFINAE). http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error has a much more detailed description.

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

Sidebar

Related Questions

Consider a template class like: template<typename ReturnType, ReturnType Fn()> class Proxy { void run()
Consider this class: class foo(object): pass The default string representation looks something like this:
Consider something like: struct Parameter { int a; Parameter(){a = 0;} void setA(int newA){a
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider something like: cat file | command > file Is this good practice? Could
I have a question regarding implementation of smart-search features. For example, consider something like
Consider a worker loop that has something like: ... auto msg = new immutable(DataWrittenMsg)(bytesWritten);
Is there a way to do something like this in c#? Consider the following
consider a query that looks something like this: my $query=<<QUERY; select * from foo1
Consider the following code: echo $this->Form->year('expiry',date('Y'),date('Y')+2) This produces output something like: 2013 2012 2011

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.