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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:29:25+00:00 2026-06-04T07:29:25+00:00

I have a lot of code in a big project that has two general

  • 0

I have a lot of code in a big project that has two general types of code, some that is done in a nice C++ style and has been code reviewed by an expert in C++ and some that is not and has not. The code that is not has lots of for loops and unchecked array (heap) accesses consisting of both reads and writes. Fortunately, all these accesses to the heap are done through a class. For argument’s sake, let’s call it CArray.

CArray is defined entirely inside header files since it’s templated. It is roughly defined as follows (only showing relevant portions):

template <typename elementType> class CArray
{
    /// Most of class details I'm leaving out

public:
    inline elementType & operator[](unsigned int i)
    {
#ifdef CARRAY_BOUNDARY_DEBUGGING
        if(i >= m_numElements)
        {
            throw SomeException("CArray::operator[] going out of bounds");
        }
#endif
        return m_pArray[i];
    }

    /// also have corresponding const version of this operator[]

private:
    elementType *m_pArray;
    int m_numElements;
}

(Please assume that the constructor, destructor, copy constructor, assignment operator and the corresponding rvalue reference versions are done right. Basically assume that the user of the class everything they need to use this class properly.

Now the question is, if I have some compilation units (.cpp files) that define CARRAY_BOUNDARY_DEBUGGING and then include this class (the ones needing code improvement/review), but others that do not (the ones that are rock solid):

  1. Is this guaranteed to be OK and not introduce problems since the class if often passed via copy, reference, and rvalue refrence (C++11 only) over compilation unit boundaries by C++03? C++11?

  2. Is there a better way to try to do this?

Edit: clarification. I know that non-inline functions must obey the one definition rule as stated in section 3.2 of the C++03 standard, but this is inline. Does the ODR still apply or is something else in effect here? Also the C++03 standard states “An inline function shall be defined in every translation unit in which it is used.”

  • 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-04T07:29:27+00:00Added an answer on June 4, 2026 at 7:29 am

    No, it’s not okay, it’s UB. Inlined definitions must be the same across the entire program. If you want to have additional logic used in some places, but not all, just make it another member (also writing CArray is silly):

    struct silly_array {
        T& operator[](int x) { /* unchecked */ }
        T& at(int x) { /* check bounds */ return (*this)[x]; }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have inherited a project that has an awkwardly big interface declared (lets call
I have a project that requires me to process a lot (1000-10000) of big
I have a big project with a lot of code (most in JSP) and
I am have a lot of code that I need to optimize and make
In my project I have a lot of code like this: int[][] a =
I have a lot of C# VS2008 code that looks like this: new SqlParameter(@Description,
I have a lot of pieces of code which has to be run one
I have a solution that is missing a lot of code coverage. I need
I am working on a Big Project and i have a lot of GWT
DirectDraw 7 is very old APIs but we have a lot legacy code written

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.