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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:45:59+00:00 2026-06-02T17:45:59+00:00

I can’t figure out why the following code compiles fine: #include <iostream> void bar(int

  • 0

I can’t figure out why the following code compiles fine:

#include <iostream>                                                                 

void bar(int x) {                                                                   
  std::cout << "int " << x << std::endl;                                            
}                                                                                   

void bar(double x) {                                                                
  std::cout << "double " << x << std::endl;                                         
}                                                                                   

template <typename A, typename B>    // Note the order of A and B.                                               
void foo(B x) {                                                                     
  bar((A)x);                                                                        
}                                                                                   

int main() {                                                                        
  int x = 1;                                                                        
  double y = 2;                                                                     

  foo<int>(x);        // Compiles OK.                                                              
  foo<double>(y);     // Compiles OK.                                               

  return 0;                                                                         
}

But if I switch the order of A and B as below, then it won’t compile:

#include <iostream>                                                                 

void bar(int x) {                                                                   
  std::cout << "int " << x << std::endl;                                            
}                                                                                   

void bar(double x) {                                                                
  std::cout << "double " << x << std::endl;                                         
}                                                                                   

template <typename B, typename A>    // Order of A and B are switched.
void foo(B x) {                                                                     
  bar((A)x);                                                                        
}                                                                                   

int main() {                                                                        
  int x = 1;                                                                        
  double y = 2;                                                                     

  foo<int>(x);        // error: no matching function for call to ‘foo(int&)’
  foo<double>(y);     // error: no matching function for call to ‘foo(double&)’                                                              

  return 0;                                                                         
}      

EDIT: Ad-hoc explanations are welcome, but would be better if someone can point out exact what the spec. says. Thanks!

  • 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-02T17:46:01+00:00Added an answer on June 2, 2026 at 5:46 pm

    In the first one, the compiler knows that A is int because you specifically tell it so with foo<int>, and it knows that B is also int because of the parameter that you pass it. So both A and B are known or can be deduced (you could say: A is supplied, B is implied).

    However, in the second one, since B comes first and A doesn’t appear in the parameter list, the compiler can’t tell what A is and gives you an error. You’re explicitly telling it what B is with foo<int>, and then the parameter you pass is also a B which, at the call, is an int which agrees with your previous explicit definition of B, but no mention is made of A, implicitly or explicitly, so the compiler must stop and error.

    You don’t really need the standard for this, it’s just common sense. What on earth would A be in the second one?

    Thanks for asking this question though, because I didn’t realise you could explicitly specify some parameters and implicitly specify others in the parameter list before this.

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

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
Can you cast a List<int> to List<string> somehow? I know I could loop through
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can anyone give me an example of how to return the following json simply
Can anybody tell what is the best(easy) way to sort the following string 'index'
can someone explain why the compiler accepts only this code template<typename L, size_t offset,
Can't work out a way to make an array of buttons in android. This
Can anyone help me trying to find out why this doesn't work. The brushes
Can we close all known/unknown connections to database with the code? I'm using Access
Can i get the source code for a WAMP stack installer somewhere? Any help

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.