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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:56:00+00:00 2026-05-28T02:56:00+00:00

I have a module that uses std::string as part of its interface. I would

  • 0

I have a module that uses std::string as part of its interface. I would like to run a unit test on this module using bounds checking on index access of the std::string. That is, if a string s has only 5 characters, I would like to throw an exception of some kind when accessing s[5] (or s[-1]).

Here is the basics of my code. The module is all implemented in a single header file.

module.h

#include <string>

int decode(const std::string &data) { ... }

test.cpp

#include "module.h"

#include "gtest/gtest.h" // using Google Test

TEST(Module, Decode) {
    ASSERT_EQ(decode("...") == 5); // or whatever
}

I suppose one way would be to implement the module.h in terms of templates, where the implementation depends on some type that allows indexing. However, there’s no reason for the implementation in module.h to use anything other than std::string in the real world; such a feature would only be used for unit testing. I’d rather not use templates here.

I’m okay with using preprocessor tricks as long as the tricks are limited to the test.cpp module. For example, I don’t want to have to #define ModuleString std::string before including module.h under normal circumstances.

What are my options here?

  • 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-28T02:56:00+00:00Added an answer on May 28, 2026 at 2:56 am

    Instead of the operator[], you could use std::string::at, which already does boundary checking and throws std::out_of_range. Feasible?

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

Sidebar

Related Questions

I have a Powershell script that uses this PowerShell module . It basically allows
We have a multi-module maven project that uses a profile that defines a buildnumber-maven-plugin
I have a Python program that uses the threading module. Once every second, my
I have a simple Python script that uses the socket module to send a
My application is written in C. I have a module that uses some data
Hi we have made a module for a controller and that module uses the
I have a reporting module (C# asp.net) that uses dynamic controls to represent the
I have a reporting module in an ASP.NET webforms app (C#) that uses dynamic
We have a mish-mash app with a legacy module that still uses DataSets, DataViews
I have a very simple Win32 application that uses CAtlExeModuleT. The module simply creates

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.