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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:15:38+00:00 2026-06-11T20:15:38+00:00

From C++ Primer, I know that for template’s parameter arguments, only two kinds of

  • 0

From C++ Primer, I know that for template’s parameter arguments, only two kinds of conversions are performed: one is const conversion, another one is array/function to pointer conversion.

However, when it comes to explicit argument, it seems that everything changes.
Assume we have a template function:

template <typename T>
int compare(const T &a, const T &b)
{
    // do comparison
}

If no explicit argument involved, function call like this is illegal:

compare("foo", "foobar");

The weird thing happens (actually, it might not be weird but I do not understand) when we explicitly do:

compare<std::string>("foo", "foobar");

It seems that in the second call, “foo” and “foobar” are converted to std::string, which is controversial.

Is there any special rules for template explicit arguments? 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-11T20:15:39+00:00Added an answer on June 11, 2026 at 8:15 pm

    In the first case the compiler tries to deduce the type T from the given parameters. From the first parameter the compiler deduces the type const char (&)[4] (aka. reference to an array of 4 characters), from the second it gets const char (&)[7]. The two types don’t match and the compiler can’t figure out what T should be.

    In the second example you explicitly specify that the T template parameter should be std::string. So T will be std::string. The compiler accepts the type you give and checks if the given function parameters match that type. In this case the parameters fit, because "foo" and "foobar" can be implicitly converted to std::string. (The const char[] degrade to const char*, and then there is a constructor that can construct a std::string from a const char*)

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

Sidebar

Related Questions

I'm trying to self-study C using C Primer Plus from Stephen Prata and one
i have a method that return a ArrayList from a sqlite query, only a
From time to time I run into the issue that Grails integration tests the
I know that cout have buffer several days ago, and when I google it,
I'm doing a programming question from C++ Primer Plus which asks me to make
A quote from the C++ Primer, 4th edition: The rules for when an exception
I got this code from the c++ primer book, which was meant to explain
I know cout and printf have buffer today, and it is said that the
I have an array of prime numbers from 2 to 997. How do you
I am trying to get all the prime(row) numbered records from my table.Can someone

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.