Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I "override" [] to accept two arguments in C++?

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?

like image 592
Nathan Fellman Avatar asked Oct 20 '10 12:10

Nathan Fellman


People also ask

What to do if a function has too many arguments?

There are two techniques that can be used to reduce a functions' arguments. One of them is to refactor the function, making it smaller, consequently, reducing the arguments' number. The Extract Method technique can be use to achieve this goal.

How many arguments can a function accept?

Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.

How many arguments can be passed to main from the command line in C?

The C language provides a method to pass parameters to the main() function. This is typically accomplished by specifying arguments on the operating system command line (console). There are two parameters passed to main().

Can we pass arguments in main () in C?

Yes, we can give arguments in the main() function. Command line arguments in C are specified after the name of the program in the system's command line, and these argument values are passed on to your program during program execution. The argc and argv are the two arguments that can pass to main function.


1 Answers

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.

like image 98
Matthieu M. Avatar answered Oct 27 '22 14:10

Matthieu M.