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

  • Home
  • SEARCH
  • 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 4054900
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:38:02+00:00 2026-05-20T14:38:02+00:00

The following code compiles fine: template<typename T> void f(const T &item) { return; }

  • 0

The following code compiles fine:

template<typename T>
void f(const T &item) { return; }

int main() 
{
  f("const string literal");
}

Compilation succeeded at ideone : http://ideone.com/dR6iZ

But when I mention the return type, it doesn’t compile:

template<typename T>
T f(const T &item) { return item; }

int main() 
{
  f("const string literal");
}

Now it gives error:

prog.cpp:6: error: no matching function for call to ‘f(const char [21])’

Code at ideone : http://ideone.com/b9aSb

Even if I make the return type const T, it doesn’t compile.

My question is :

  • Why does it not compile?
  • What does the return type has to do with the error and the function template instantiation?
  • 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-20T14:38:03+00:00Added an answer on May 20, 2026 at 2:38 pm

    You cannot return an array from a function, so template instantiation fails, and there’s no matching function.

    You get this particular error because of SFINAE – It’s not really an error that the compiler cannot instantiate your function, it is an error that there’s no matching function.

    You can return a reference to an array – returning T const & will work.

    EDIT: In response to comments:

    First, this is actually a decent example of SFINAE.

    template<typename T> T f(const T &item) { return item; }
    char const * f(void const * item) { return 0; }
    int main() {
      f("abc");
    }
    

    When the compiler compiles this, it’ll first try to instantiate the templated f, to create an exact match for the type const char [3]. This fails, because of the mentioned reasons. It’ll then select the inexact match, the plain function, and in the call decay the const char [3] to a const char *.

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

Sidebar

Related Questions

The following code compiles and runs fine. void myInvokedMethod(string s) { Console.WriteLine(s); } void
The following code does not compile: public class GenericsTest { public static void main(String[]
In C++ the following code gives a compiler error: void destruct1 (int * item)
In Microsoft Oslo SDK CTP 2008 (using Intellipad) the following code compiles fine: module
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
What is wrong with the following piece of code? template<typename X> struct A {
the following code compiles fine under gcc: class vec3 { private: float data[3]; public:
This question is related to this question. The following code compiles fine VC9 compiler
The following code compiles fine. #define CMD_MACRO(pp, cmd) \ { \ if (pp)\ {
The following code compiles and runs fine with gcc. But i wondered if such

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.