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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:22:19+00:00 2026-06-13T21:22:19+00:00

I have been trying to understand this code template <typename T, typename _Prd =

  • 0

I have been trying to understand this code

    template <typename T, typename _Prd = equal_to<T> >
    struct Vector3
    {
    protected:
        T i,j,k;
        _Prd comp;
    public:
        //default constructor
        Vector3(void);

        //explicit constructor
        Vector3(const T& ijk);

        //explicit constructor
        Vector3(const T& i,const T& j,const T& k);

        //copy constructor
        Vector3(const Vector3<T,_Prd>& copy_from);

        //getters
        T I() const;
        T J() const;
        T K() const;

        //setters
        void I(const T& i);
        void J(const T& j);
        void K(const T& k);

        //get magnitude of vector.
        T Magnitude() const;

        //angle between I,J (clockwise)
        T Direction() const;

        //angle between K and I,J
        T Elevation() const;

        //scale vector to 1
        Vector3<T,_Prd>& Normalize();

        //vector scale up-to value
        Vector3<T,_Prd>& Scale(const T& length);

        ...
};

I can’t understand first statement

template <typename T, typename _Prd = equal_to<T> > struct Vector3 {};

It’s about the usage of equal_to<T>, I found the reference from here and here. But still there isn’t anything like this. Thank you for any help to make me understand this part.

Update:

After seeing the answers and reading some text book, my question turns to 2 aspects.

1. Default Template Arguments

In C++11 we can supply default template arguments to a template.
Example from C++ primer 5th ed. Page 670.

#include <functional>
template <typename T, typename F = less<T> >
int compare (const T &v1, const T &v2, F f = F() )
{
    if (f(v1, v2) return -1 ;
    if (f(v2, v1) return 1 ;
    return 0 ;
}

And then we use this template as:

bool i = compare(0,42) ; 

The template will use default less function-object class to instantiate.
However, when we use our own objects:

Sales_data item(cin), item2(cin) ;
bool j = compare (item, item2, compareIsbn) ;

Then F turns to compareIsbn function-object instead.
So that the same happens on my question above, this way will leave an entrance to the user of the template to allow them introducing their own function-object, in this case it is used as comparator.

2. Predicates

Check What is predicate in C++?

  • 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-13T21:22:20+00:00Added an answer on June 13, 2026 at 9:22 pm

    std::equal_to is a class template, which provides something like this:

    bool operator()(T const & lhs, T const & rhs) const { return lhs == rhs; }
    

    In other words, it’s a function-object class that wraps the ordinary == comparator. The point is that equal_to can be specialized, and thus provides a non-intrusive way of adding customizability.

    On top of that, your template Vector3 also provides an intrusive way of customizing the comparator by way of the second template argument.

    Note that predicates are typically objects, and not just trait classes. So your container will actually contain a predicate subobject. If the predicate class is default-constructible, this is no problem, but if it isn’t then you must normally provide a copy of the predicate in the container constructor.

    As a homework assignment, you can think about how you can avoid spending any actual memory on the predicate subobject.

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

Sidebar

Related Questions

okay i have been trying to understand this for hours i am learning VB
i have been trying to understand the pack function. I'm having trouble on this
I have been trying to get this code to work for a while now
I have been trying to understand the way ActionScript's events are implemented, but I'm
So, I have been trying to understand Socket.io lately, but I am not a
I've been trying hard to understand this, but can't quite put a finger on
I have been trying to get some basic dynamic code compilation working using the
I have been trying to solve this problem for three days now, it's really
I have been trying to teach myself MEF, starting with this tutorial: http://blogs.msdn.com/b/brada/archive/2008/09/29/simple-introduction-to-composite-applications-with-the-managed-extensions-framework.aspx There
I've been trying to understand how to fix this error: SEVERE: Missing dependency for

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.