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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:52:36+00:00 2026-05-29T03:52:36+00:00

I was working on some template code this morning where I used a BOOST_STATIC_ASSERT

  • 0

I was working on some template code this morning where I used a BOOST_STATIC_ASSERT to ensure I wasn’t creating a reference to the wrong type, as I thought it might be a clearer error message. However when I tried removing the static assert to take a look at the alternative compiler error I was shocked to discover that gcc doesn’t even complain when you try to make a const double& referring to an int:

#include <iostream>

int main()
{
    int x = 5;
    const double& y = x;
    std::cout << y << std::endl;

    return 0;
}

Compiles, and doesn’t even warn:

$ g++ ~/stuff/badRef.cpp -Wall -Wextra -pedantic
$ a.out
5

What’s going on here? Is this undefined behaviour? If so why doesn’t gcc complain? On my machine int is 4 bytes and a double is 8. That means that when printing a double& it should interpret 8 bytes at that address as a double and print it, yet there is actually a 4 byte int at that location.

Very confused. Help!

  • 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-29T03:52:36+00:00Added an answer on May 29, 2026 at 3:52 am

    const double& y = x; creates a temporary double with the value static_cast<double>(x), then binds that temporary to y. The lifetime of the temporary is extended to match the lifetime of y.

    This is completely legal C++ (03 and 11), hence the lack of warning/error.

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

Sidebar

Related Questions

Whilst working on some generally horrible Javascript code this morning, I came across the
I have some template/starting point code that I reuse across projects. While working on
I have a piece of code working in knockout.js like this: <div> ... some
I have been working on some legacy C++ code that uses variable length structures
I'm working on some code to colorize an image in Java. Basically what I'd
I'm working on some code that uses the System.Diagnostics.Trace class and I'm wondering how
I'm working on some code for a loosely coupled cluster. To achieve optimal performance
I'm working with some code that is confusing me and I'm wondering if I'm
I have some working Javascript code that generates an RDF/XML document using variables picked
I am working through some C++ code from Financial Instrument Pricing Using C++ -

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.