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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:22:37+00:00 2026-06-18T23:22:37+00:00

I’m writing a C++ class. Some of its fields are STL containers, some aren’t.

  • 0

I’m writing a C++ class. Some of its fields are STL containers, some aren’t. While writing the methods I began wondering, how should I handle invalid values passed to methods? For example, some methods are more or less wrappers to STL container methods. Many STL methods just have “undefined behavior” when invalid iterators are passed. I guess it’s like that because it allows the STL code to ignore these cases and thus be faster.

But for higher level code, what should I do? I do throw exceptions when an unexpected error occurs, for example an error made by a mistake a developer made. But in this case the parameter value depends on the interface user, not on the implementor. I could ignore invalid parameters and invalid iterators, etc. and “pass” the problem to lower-level functions, which would then have undefined behavior, but I could also throw an exception or at least find some way to report an error.

What would be the best thing to do?

Example: I have a class representing a tree node and it has a method add_child() which takes a std::shared_ptr parameter. Should I check the value or let the user make sure a nullptr isn’t passed? Or for an invalid iterator, pass it to STL methods or report an error? If I should report – are exceptions the right solution?

  • 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-18T23:22:39+00:00Added an answer on June 18, 2026 at 11:22 pm

    I think a universal answer cannot be given. Too many things affect the decision that you haven’t mentioned: first of all, are you developing a library or an application?

    In the first case, flexibility should be favored and the best choice is usually to design your interface in a way that leaves the decision up to the client. Since you don’t know what clients will use your class and which requirements they will have, it’s best to make as few assumptions as possible. If your clients have tight performance requirements, they won’t be willing to pay the performance penalty of an extra bounds check, for instance. This is, basically, the reason why bound checks are not performed by STL either.

    In the second case, it depends on the performance requirements of your application and the exception safety guarantees your class has to provide. Which operations need to be no-throw? Are you allowing or planning to use your class inside an STL container? Is it going to be used in the most critical part of your application, so that a small performance performance gain in the algorithms of this class will affect the overall execution time of your application?

    There is no such a thing as a “universally best design”. Engineering is all about finding the right compromise according to the particular situation at hand, and software engineering is no exception to this.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload
Let's say I'm outputting a post title and in our database, it's Hello Y’all

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.