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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:20:53+00:00 2026-06-14T09:20:53+00:00

Sometimes when I’m programming in C++ I wish there was an undefined value for

  • 0

Sometimes when I’m programming in C++ I wish there was an undefined value for every variable something like Javascript!.
For example when I’m returning a value for out-of-bounds element of an array, it was useful to return an undefined instead of throwing an exception, or:

template <typename T, int SIZE>
class MyArray
{
  T arr[SIZE];
  static T badref;
public:
  T &operator[](int i)
  {
    if (i >=0 && i < SIZE)
      return arr[i];
    else
      throw std::string("OUT-OF-BOUNDS"); // or: return badref; !!
  }
};

Another dirty(In my opinion) option is returning a reference of a pre-defind variable as a bad-reference variable. I know we can not assign null or something like that to a reference variable.

Is there an another well formed pattern to return a reference where caller has the ability to find out the returned value is not valid?

EDIT: I’m not mean a pointer

  • 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-14T09:20:54+00:00Added an answer on June 14, 2026 at 9:20 am

    You can use boost::optional as @chris mentioned in his comment. It comes as a part of Boost libary. See this page for more details.

    Modified MyArray class:

    template <typename T, int SIZE>
    class MyArray
    {
      T arr[SIZE];
    public:
      optional<T&> operator[](int i)
      {
        if (i >=0 && i < SIZE)
          return optional<T&>(arr[i]);
        else
          return optional<T&>();
      }
    };
    

    Usage:

    MyArray<int>() array;
    // fill array with data
    
    optional<int&> result = array[0];
    if (result) {
        // item was found
    } else {
        // index out of bounds
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes, when I look through my header files I'd like to add something little
Sometimes you see code like this. <script type=text/javascript src=https://apis.google.com/js/plusone.js> {lang:'en', parsetags:'explicit'} </script> I'd like
Sometimes i face the following problem : string txt = con.Request.Params[Par_name].ToString();//the original par value
Sometimes I see code like this: LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL&
Sometimes I'd like to print my code and read it during lunch. In Eclipse
Sometimes for example I change a word in line 100 and delete last 4
Sometimes I get an exception like this: This method cannot be translated into a
sometimes i see that certain projects write something to the output during compilation. how
Sometimes when I'm debugging some javascript in Chrome and I have the javascript paused,
Sometimes i wonder, when converting strings to other types, for example int32, is one

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.