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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:11:53+00:00 2026-05-17T20:11:53+00:00

I am trying to create a bit-vector class in C++ to model some hardware.

  • 0

I am trying to create a bit-vector class in C++ to model some hardware. In most HDLs (hardware description langauges) that I know, specific bits are referenced like this:

my_vector[bit_position]

and sub-vectors are referenced like this:

my_vector[msb:lsb]

or

my_vector[msb,lsb]

I want to be able to do something similar with my bit-vector class. Is there any way to tell operator[] to accept two arguments?

The alternatives I’ve considered are:

  1. using a range method:

    my_vector.range(msb,lsb)

  2. using a string and parsing it:

    my_vector["msb:lsb"]

But neither of them is attractive. The first, because it is too different from the way it’s modeled in HDL, the second because I don’t like dealing with strings when I don’t have to, and it seems inelegant.

What’s the best way to do this?

  • 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-17T20:11:53+00:00Added an answer on May 17, 2026 at 8:11 pm

    The issue:

    Apart from operator() all operators have a fixed arity, which effectively precludes any kind of change

    You then have several solutions:

    • overload operator() instead: vector(msb, lsb)
    • use two successive invocations: vector[msb][lsb]
    • overload the comma operator: vector[msb,lsb]

    The last solution matches the syntax you require, but is somewhat subtle:

    • you first need either msb or lsb to be of a custom type (for operators cannot be overloaded on built-ins only)
    • you then provide an overload of operator, for this type, returning a Range object
    • you finally provide a custom operator[](Range) on your class

    The real bummer is the first point: that one of msb or lsb need be of a custom type. This can be somewhat alleviated using Boost.StrongTypedef which creates a custom type that mimicks an existing one.

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

Sidebar

Related Questions

I'm trying to create some HtmlHelper extensions and ran into a bit of a
I'm trying to create a simple flex4 project which involves some timers that trigger
I'm trying to recreate the inbuilt vector class in cpp to get a bit
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create my first iPhone app that would play back audio. When I
Trying to create a small monitor application that displays current internet usage as percentage
Trying to create a list to return some JSON data to a view. Following
I'm trying to create a template class, and when I define a non-member template
I'm trying to find some way to substract a size 3 vector from each
I'm trying to create tables with rounded corners. I've quite a bit of XML

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.