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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:42:14+00:00 2026-06-09T17:42:14+00:00

In Stroustrup’s C++ Programming Language book (3rd edition), in the Numerics chapter he shows

  • 0

In Stroustrup’s C++ Programming Language book (3rd edition), in the Numerics chapter he shows the following code snippet:

void f(valarray<double>& d)
{
    slice_array<double>& v_even = d[slice(0,d.size()/2,2)];
    slice_array<double>& v_odd = d[slice(1,d.size()/2,2)];

    v_odd *= v_even;
    v_even = 0;
}

The problem is, v_even and v_odd are non-const references to temporaries, which isn’t allowed. And attempting to compile this emits an error:

error: non-const lvalue reference to type 'slice_array<double>' cannot bind to a temporary of type 'slice_array<double>'
    slice_array<double>& v_even = d[slice(0,d.size()/2,2)];
                         ^        ~~~~~~~~~~~~~~~~~~~~~~~~

I checked through all of the errata available online and there’s nothing that touches upon this fundamental problem. Am I missing something? Did the language change in this regard since the book was printed (unlikely, since the book itself mentions the rule against non-const references to temporaries)? What’s going on here?


If I modify the function to use values instead of references, e.g. slice_array<double> v_even = ..., then this actually compiles. However, it turns out my local C++ headers make the copy constructor public, whereas Stroustrup and various online references (cppreference.com, cplusplus.com) claim the copy constructor is private. I assume that means this solution is non-portable. This is reinforced by the fact that Stroustrup explicitly lists a code sample with non-reference variables and says this produces an error.


The C++98 spec (PDF) declares slice_array<T> as having a private copy constructor. By 2005 (according to this spec), and presumably as part of C++03, this changed to a public copy constructor.

  • 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-09T17:42:15+00:00Added an answer on June 9, 2026 at 5:42 pm

    There seem to be a couple of different issues with the original code sample, and also the declarations given in the book for a number of operators.

    The ‘best’ solution I believe is to do as follows

    void f(valarray<double>& d)
    {
        const slice_array<double>& v_even = d[slice(0,d.size()/2,2)];
        const slice_array<double>& v_odd = d[slice(1,d.size()/2,2)];
    
        v_odd *= v_even;
        v_even = 0;
    }
    

    All operators on slice_array<T> are defined as const as they are not modifying the slice itself, but the contents. These are defined incorrectly in the book as non-const.

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

Sidebar

Related Questions

In Stroustrup's The C++ Programming Language: Special Edition (3rd Ed) , Stroustrup writes that
In Bjarne Stroustrup's book The C++ Programming Language, it is stated that a derived
In his The C++ Programming Language Stroustrup gives the following example for inc/dec overloading:
On page 340 of the C++ Programming Language: Special Edition, Stroustrup writes... The semantic
I've been working through Bjarne Stroustrup's The C++ Programming Language (2nd edition - I
I'm studying The C++ Programming Language from Bjarne Stroustrup and he talks about logical
I'm reading Stroustrup's Programming: Principles and Practice and came across this code: int main()
Stroustrup states in C++ Language book that order of definitions in the class does
This is taken right from The C++ Programming Language by Bjarne Stroustrup. I would
I'm doing the exercises in Stroustrup's new book Programming Principles and Practice 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.