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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:16:37+00:00 2026-06-17T13:16:37+00:00

In the simple following example, I expected the output would be 2222. But the

  • 0

In the simple following example, I expected the output would be “2222”. But the actual output was “2122” for both VC++ 11.0 and g++ 4.6.1.

#include <iostream>

template <class T>
void func(T x)
{
    x = 2;
    std::cout << x;
}

int main()
{
    int x = 1;

    func((int &)x);
    std::cout << x;

    func<int &>(x);
    std::cout << x;

    return 0;
}

I disassembled and found that the first func call, func((int &)x), uses func<int> instead of func<int &>. Why and how is this happened?

  • 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-17T13:16:38+00:00Added an answer on June 17, 2026 at 1:16 pm

    Template type argument deduction works that way. The cast to int& has no effect, as the variable x is already an lvalue. Template type deduction when the argument is an lvalue and the parameter is not a reference will deduce the type not to be a reference.

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

Sidebar

Related Questions

Consider the following example: #include <iostream> #include <iostream> #include <type_traits> template<typename Type, template<typename> class
I have a big list, but micro example would be like the following: A
The following simple test is failing: assertEquals(myStringComingFromTheDB, £); Giving: Expected :£ Actual :£ I
While doing the following simple example, I found the following difficulties As the title
I'm following this example for creating a simple Web application using JAX-RS, MongoDB and
I created the following simple PL/SQL stored procedure example to ask a specific question.
Using the following code: use LWP::Simple; my $url= http://example.com; my $html= get $url; I
Bare with the following, as I tried to think of a simple example and
Using re.sub in Python 2.7, the following example uses a simple backreference: re.sub('-{1,2}', r'\g<0>
I'm trying to get a super simple GLFW and VBO example running, but I'm

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.