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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:28:45+00:00 2026-05-13T08:28:45+00:00

I was writing a test case out to tackle a bigger problem in my

  • 0

I was writing a test case out to tackle a bigger problem in my application. I ended trying some code out on codepad and discovered that some code that compiled on my local machine (g++ 4.4.1, with -Wall) didn’t compile on codepad (g++ 4.1.2), even though my local machine has a newer version of g++.

Codepad calls this a reference to reference error, which I looked up and found a litle information on. It looks like it’s not a good idea to have a stl container of references. Does this mean I need to define my own PairPages class? And if this is the case, why did it compile locally in the first place? What’s going on?

codepad link: http://codepad.org/UAaJI1rl

#include <deque>
#include <utility>
#include <iostream>

using namespace std;

class Page {
  public:
    Page() : number_(++count) {}
    int getNum() const { return number_; }
  private:
    static int count;
    int number_;
};

int Page::count = 0;

class Book {
  public:
    Book() : currPageIdx_(3) {
      int numPages = 5;
      while (numPages > 0) {
        pages_.push_back(Page());
        numPages--; // oops
      }
    }
    pair<const Page&, const Page&> currPages() { return pagesAt(currPageIdx_); }
    pair<const Page&, const Page&> pagesAt(int pageNo) { return make_pair(pages_[pageNo - 1], pages_[pageNo]); }
    //const Page& currPages() { return pagesAt(currPageIdx_); }
    //const Page& pagesAt(int pageNo);
  private:
    deque<Page> pages_;
    int currPageIdx_;
};

int main() {
    Book book;
    cout << book.pagesAt(3).first.getNum() << endl;
    cout << book.currPages().first.getNum() << endl;
}
  • 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-13T08:28:45+00:00Added an answer on May 13, 2026 at 8:28 am

    A vector (or any STL container) of references is indeed a bad idea, as obvious when you simply look at requirements for element type T of any STL container (ISO C++03 23.1[lib.container.requirements]). It starts off by saying that “containers are objects that store other objects”. We can stop right here, because a reference is not an object in C++ (unlike, say, a pointer; note that “object” in C++ parlance doesn’t mean “instance of class”!). But, furthermore, it requires T to be Assignable, the requirements for which refer to type T& – if T is itself some reference type U&, then the constructed type would be U& &, which (reference to reference) is illegal in C++.

    If you really want to have a container that doesn’t manage lifetimes of objects, then you should use a container of pointers. If you prefer the safety of references (e.g. lack of pointer arithmetic and null value), you can use std::tr1::reference_wrapper<T> class, which is copy constructible and assignable wrapper for a reference.

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

Sidebar

Related Questions

Does anyone have some good hints for writing test code for database-backend development where
I am writing a test program to copy some file in Application data folder
I am writing an unit test for a mvc web application that checks if
I am writing a test case against a controller that returns a pdf file.
Writing some test scripts in IronPython, I want to verify whether a window is
I'm writing a unit test for a method that packs boolean values into a
I am writing a few test cases that depend on the database being available,
I'm writing a test for a file parser class. The parse method receives a
I am writing a small app which I need to test with utf-8 characters
I am writing a silly little app in C++ to test one of my

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.