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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:12:31+00:00 2026-05-31T02:12:31+00:00

I have a struct with these operators (given some type T ): T& operator[](size_t

  • 0

I have a struct with these operators (given some type T):

T& operator[](size_t i) { return write(i); }
const T& operator[](size_t i) const { return get(i); }

When I access a non-const object of this by [i], will it always use the non-const operator[] or will it automatically see which one is needed (e.g. if the const-version is “enough”) and take that one? What are the rules there? And do most compilers (I mostly care about Clang, recent GCC and recent MSVC) behave the same there?

Background: write(i) could be much more costly than get(i). I even have some code where the behavior is slightly different, where write(i) might touch some files, set some modified-flags or whatever.

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

    As with any member function, the choice of overload depends on the type of the implicit instance argument:

    T x;
    
    x.foo();                        // #1
    const_cast<T const &>(x).foo(); // #2
    

    In case #1, the implicit instance argument has type T, so the overload T::foo() is viable and will be selected. In case #2, the implicit instance argument has type T const, and thus only an overload T::foo() const is viable, and will be selected if it exists.

    The normal overload resolution rules are at play here: If you have both const and non-const overloads and the instance argument is non-constant, then the non-constant version is chosen because it requires zero conversions, while the const overload would require one (standard) conversion, namely from T & to T const &, and thus that overload is not as good a fit as the non-const one.

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

Sidebar

Related Questions

I have: struct DoubleVec { std::vector<double> data; }; DoubleVec operator+(const DoubleVec& lhs, const DoubleVec&
Suppose I have this struct: struct vector_data { double x, y; double& operator[](size_t index)
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
Given the following: struct Foo { int bar() const; }; struct IsEqual : public
Given the following: struct Foo { int bar() const; }; struct IsEqual : public
Following code is in simplest form: struct X { operator char () const {
#include <iostream> using namespace std; struct Y; struct X { X(const Y&) { cout
I have a container like this: // Sort functor struct SortByTime : std::binary_function<const TimeSortableData
Suppose I have: struct Magic { Magic(Foo* foo); Magic(Bar* bar); }; Is there a
Ok so I have struct like this typedef struct { float x; float y;

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.