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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:03:48+00:00 2026-05-29T11:03:48+00:00

The following C++ is invalid because reference variables require initializers: int& a; // illegal

  • 0

The following C++ is invalid because reference variables require initializers:

int& a; // illegal
if (isfive) {
  a = 5;
} else {
  a = 4;
}

However, MSVC seems to think this is okay:

int& a = isfive ? 5 : 4;

This implies to me that MSVC is actually treating the conditional operator like a single expression and not expanding it into an if-else statement.

Is it always valid C++ to initialize a reference using the conditional operator?

  • 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-29T11:03:48+00:00Added an answer on May 29, 2026 at 11:03 am

    MSVC has a non-standard “extension”. What it means is that it allows broken code. There’s a good reason this is prohibited.

    Note also that

    int& a = 5;
    

    is not legal in Standard C++ either.

    In general, though, it is legal to initialize a const reference with any expression which can be converted to the right type (including use of the conditional operator). And it is legal to initialize a non-const reference with an lvalue of the right type, which the conditional operator yields under certain conditions.

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

Sidebar

Related Questions

This following code gets this compile error: " invalid types 'int[int]' for array subscript
SAX keeps on dying on the following exception: Invalid byte 2 of 3-byte UTF-8
Like the title says, the error I get is the following: An invalid '/'
The following PL/SQL will not execute and simply returns 'Invalid Argument' as an error.
The following WMI query throws an exception at serverProtocolsManagement.Get() when sqlHost is an invalid
I used the following code to invoke an invalid parameter handler if the user
I have PHP code snippet the following: if (!($result = mysql_query($query, $link))) { die(Invalid
When running a batch import script in Python (openblock), I'm getting the following invalid
I have the following MyType::Is_Inst () function which is throwing an invalid memory access
Why is the following code valid: template<typename T1> void foo(T1 &&arg) { bar(std::forward<T1>(arg)); }

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.