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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:13:56+00:00 2026-06-15T23:13:56+00:00

In the following example, Foo is not doing what is intended, but I can’t

  • 0

In the following example, Foo is not doing what is intended, but I can’t figure out why this is allowed to compile.

#include <string>
#include <iostream>

typedef std::string& T;

T Foo(int & i)
{
    return T(i);
}

int main()
{
    int a = 1;
    std::string & s = Foo(a);
}

I discovered this with templates, but the typedef shows that its unrelated to templates. Needless to say, s is not a valid string here. I would think that constructing the value in the return of Foo would produce a compile error.

What am I missing here?

  • 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-15T23:13:57+00:00Added an answer on June 15, 2026 at 11:13 pm

    First of all, it worth nothing that the problem actually has no relationship to templates because this code compiles a well:

    typedef std::string& T;
    T Foo(int& i) {
        return T(i);
    }
    

    The reason I think this compiles is that the return statement is equivalent to

    return reinterpret_cast<T>(i);
    

    in case T happens to be a reference members. … and this, of course, compiles: You promised you knew what you were doing and asked the compiler kindly to believe you.

    OK, found it at 5.2.3 [expr.type.conv] paragraph 1:

    … If the expression list is a single expression, the type conversion expression is equivalent (in definedness, and if defined in meaning) to the corresponding cast expression (5.4). …

    … and 5.4 [expr.cast] paragraph 4:

    The conversions performed by [other forms of casts] a reinterpret_cast (5.2.10) […] can be performed using the cast notation of explicit type conversion. […]

    (the elisions cover cases involving user defined type, built-in type conversions, const conversions, etc.)

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

Sidebar

Related Questions

This is just an example, but given the following model: class Foo(models.model): bar =
The following example will not compile for me: #include <iostream> #include <functional> #include <string>
Is the following example appropriate for PHP's require_once construct? function foo( $param ) {
I find the following example mildly surprising: >>> class Foo: def blah(self): pass >>>
Example: Suppose in the following example I want to match strings that do not
Taking the following snippet as an example: struct Foo { typedef int type; };
This is what I'm doing: @XmlType(name = foo) @XmlAccessorType(XmlAccessType.NONE) public final class Foo {
I'm not sure if this is an odd setup to begin with, but here's
The following example demonstrates the problem I encountered in VC++ 2010: class Foo {
Another C++ pointer deletion question is in the following example: class Foo { public:

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.