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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:40:28+00:00 2026-06-13T02:40:28+00:00

I wrote a template to have a valid usage only when a struct ,

  • 0

I wrote a template to have a valid usage only when a struct, class has overloaded bool operator== otherwise compiler errors would come up,

namespace atn {
    template <typename T>
    bool find( std::vector<T>& cont, T find ) {
        for( std::vector<T>::iterator it = cont.begin(); it != cont.end(); ++it ) {
            if( (*it) == find )
                return true;
        }
        return false;
    }
};

So fine it is ok, for example:

struct sPlayer {
    u_int idPlayer;
    sPlayer() : idPlayer(0) {};
    bool operator==( const sPlayer& ref ) const {
        return ref.idPlayer == this->idPlayer;
    };
};

int _tmain(int argc, _TCHAR* argv[]) {
    std::vector<sPlayer>a;
    sPlayer player;
    player.idPlayer = 5;
    a.push_back(player);
    if(atn::find(a, player)){
        std::cout << "Found" << std::endl;
    }
    return 0;
}

The thing is, if I use it this way:

vector<int>hold;
if(atn::find(hold, 4))

I got lost at this part, the templates assumes the type of T to be assigned at vector<T> by the value of the second parameter passed? Or it’ll assume from the type of the vector reference passed?

  • 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-13T02:40:30+00:00Added an answer on June 13, 2026 at 2:40 am

    Both arguments have to match. Template argument deduction tries to find a type for each template argument that makes the function argument types match the type of the supplied arguments.

    Sometimes this gets a bit tricky, and things that should work don’t. For example:

    std::vector<int> v;
    atn::find(v, 1U);
    

    This fails, because the first argument wants to deduce T = int, but the second argument wants T = unsigned int. The deduction fails and the code doesn’t compile. (If this is a problem, then the solution is to make all but one function argument non-deduced.)

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

Sidebar

Related Questions

I have a simple class: template<size_t N, typename T> class Int { bool valid(size_t
I wrote the following program #include <iostream> template<typename C, typename Res, typename... Args> class
I wrote this little code std::map<int,template<class T>> map_; map_.insert(make_pair<int,message>(myMsg.id,myMsg)); but the compiler doesn't seem
I have wrote some class, named as Employe. Employees collection I set as source
I have a set of documents based on a LaTeX template. Every document has
I have created a Custom List Template and wrote the Schema.xml,feature to deploy it,Installed
Suppose I have a function that has a parameter that is overloaded by many
I have a template that displays the list of items. It has one checkbox
I have a template class where typename is a pointer to a function: CHook<NTSTATUS(__stdcall*)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,HANDLE,PCLIENT_ID,PCONTEXT,PUSER_STACK,BOOLEAN)>
I have a matrix class like below: template <size_t M, size_t N, typename T>

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.