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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:52:35+00:00 2026-05-15T10:52:35+00:00

the following code is an example of something I’m trying to do in a

  • 0

the following code is an example of something I’m trying to do in a large project:

#include <iostream>
#include <vector>

// standard template typedef workaround
template<typename T> struct myvar {typedef std::vector<T> Type;};

template<typename T>
T max(typename myvar<T>::Type& x)
// T max(std::vector<T>& x)
{
  T y;
  y=*x.begin();
  for( typename myvar<T>::Type::iterator it=x.begin(); it!=x.end(); ++it )
    if( *it>y ) 
      y=*it;
  return y;
} 

int main(int argc, char **argv)
{
  myvar<int>::Type var(3);
  var[0]=3;
  var[1]=2;
  var[2]=4;
  std::cout << max(var) << std::endl;
  return 0; 
} 

When I try to compile it I get:

>g++ delme.cpp -o delme
delme.cpp: In function ‘int main(int, char**)’:
delme.cpp:25: error: no matching function for call to ‘max(std::vector<int, std::allocator<int> >&)’

However, if I comment out line 8 and uncomment line 9 it compiles properly and gives:

>g++ delme.cpp -o delme
>./delme 
4

Can someone please explain why the function template definition of max() using typename myvar<T>::Type& isn’t considered as a match for ‘max(std::vector<int, std::allocator<int> >&)’ and is there a way to get it to match without using the underlying std::vector<T>& type?

  • 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-15T10:52:35+00:00Added an answer on May 15, 2026 at 10:52 am

    Deducing the enclosing type (or any attributes of the enclosing type) from its the nested type is impossible. It is one of the examples of so called non-deduced context in C++.

    While the template function declaration itself is legal, you won’t be able to call your function as mere max(var) since the template argument deduction cannot be carried out. In your case, the compiler will not be able to figure out that parameter x of type std::vector<int> implies that T is myvar<int>.

    You’ll always have to specify template arguments for max explicitly, as in

    std::cout << max<int>(var) << std::endl;
    

    How to work around this problem depends on what you were trying to achieve by introducing that enclosing myvar template.

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

Sidebar

Related Questions

I'm trying to compile the following simple DL library example code from Program-Library-HOWTO with
The C# 3.0 spec has the following code example in section 10.6.1.3 Output parameters:
Take the following code for example; if (Convert.ToString(frm.WindowState) == Minimized) Layout.WindowState = Maximized; else
I have the following example code: class A(object): def __init__(self, id): self.myid = id
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
The following code should find the appropriate project tag and remove it from the
In the following code example, why does the second (sheetplus) method seem to pick
First, an example of something that works as expected: (all code was executed in
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
The following code works great in IE, but not in FF or Safari. I

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.