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 converting the following example code to Delphi: http://msdn.microsoft.com/en-us/library/bb176406%28v=office.12%29.aspx My code is something like:
I'm trying to learn LISP and was going through a code example where something
In the following code example how do the user/userParam references relate to the Customer
Please forgive the verbosity of the following code example. Using Delphi 2009, I created
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
In the following (working) code example, the templated register_enum() function is used to iterate
When I execute this following code for example: cart.php?p=1&action=add <?php session_start(); if (isset($_GET['p']) &&
The following code is an example of what I think would qualify as pseudocode,
Please consider the following example code (from the lm doc): ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt
The following Microsoft example code contains the following: <Grid> ... <Border Name=Content ... >

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.