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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:51:00+00:00 2026-05-16T08:51:00+00:00

I am currently struggling to get the following code to compile. First the header

  • 0

I am currently struggling to get the following code to compile. First the header file containing a class with a method template:

// ConfigurationContext.h

class ConfigurationContext
{
    public:
    template<typename T> T getValue(const std::string& name, T& default) const
        {
            ...
        }
}

Somewhere else I want to call this method like this:

int value = context.getValue<int>("foo", 5);

There I get the following error:

error: no matching function for call to 'ConfigurationContext::getValue(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int)'

I checked the obvious errors like missing includes and stuff like that. But everything seems to be right. I tried removing the pass-by-reference of the template type argument like this:

template<typename T> T getValue(const std::string& name, T default) const ...

Then it compiles without any errors and also runs fine, but I’d still like to pass in a reference here…

Does anybody know whats happening here and how to make this work?

  • 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-16T08:51:01+00:00Added an answer on May 16, 2026 at 8:51 am

    5 is a literal, and you cannot bind literals to non-const references. Either take T per copy or per const reference:

    template<typename T> T getValue(const std::string& name, const T& def) const
    

    (BTW, I doubt that your compiler accepts T default, because default is a keyword and must not be used as an identifier.)

    The reason you cannot do this is because taking arguments per non-const reference usually implies that the callee might change the value and such changes should reflect at the caller’s. (See How to pass objects to functions in C++?) However, you cannot change literals or temporaries. So you are not allowed to pass them to non-const references.

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

Sidebar

Related Questions

I am currently struggling with a circular dependency problem when designing my classes. Ever
I am currently struggling with whether or not I've achieved a good level of
I'm currently struggling on getting my CheckBox es to properly center within my GridViewColumn
I'm trying to get to learn som javascript/jQuery stuff, and I'm really struggling with
Currently my repository returns an IQueryable object that lists the data from my DB
I'm struggling to parse the output of the time command in bash - and
I have a table of items linked to a table of keywords. I want
could really use your help, understanding this complex rails issue I've been banging by
I'm trying to wrap my mind around F# so I thought it would be
I'm building a web-based productivity application that has to deal with modest user concurrency,

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.