Shorthand access.

Hello,

I have a piece of code that basically just toys around with two arrays. Most of the operations are swapping and copying values. To reduce the amount of typing (I know that's a bad reason, but I don't feel it reduces clarity), I'd like to add shorthand access functions.

In short, I want to replace statements like this:
array1[array2[i]] = array2[i];
with a shorter version like this:
a(b(i)) = b(i);
(These names are placeholders; in the real code both the array names and shorthand names make sense.)

It's easy enough to do:
inline int& a(const int i) const { return array1[i]; }

However, for some reason it's triggering my spidey-senses. It might be the recent contact with Java that makes me paranoid about reference-assignments. I opted to add a second version that returns by value as well. Still, I'm not entirely at peace with this.

Thus, my question:
a) Is it safe? Am I introducing new problems, or is the shorthand version completely equal to the full version?
b) Is there a reason to add the by-value version, or can I drop it?
c) Is this a Good Idea? If not, why?
look kinde weird to me.
I would do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#define a(x)	(array1[x])
#define b(x)	(array2[x])

int main( void )
{ 

	int array1[5] = {0,0,0,0,0};
	int array2[5] = {0,0,0,0,2};

	a( b(4) ) = b(4);
	cout << a(2);

    return 0;
}


2
Last edited on
Thanks for the suggestion. I'm not a big fan of defines though. I'd rather avoid using them.
Your code will work fine but I would expect a const function to return a const reference. You should probably have both a const and non-const version. The const version could return by value if you like.

Jikax's code with pointers instead of defines:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	int array1[5] = {0,0,0,0,0};
	int array2[5] = {0,0,0,0,2};

	int* a = array1;
	int* b = array2;

	a[b[4]] = b[4];
	cout << a[2];

	return 0;
}
Last edited on
If you want to use a shorter alias for something... just use a reference (which is an alias) and be done with it.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	int array1[5] = {0,0,0,0,0};
	int array2[5] = {0,0,0,0,2};

	int (&a)[5] = array1;
	int (&b)[5] = array2;

	a[b[4]] = b[4];
	cout << a[2];

	return 0;
}


A typedef could be used to make that less ugly.

Or, if your compiler supports auto:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	int array1[5] = {0,0,0,0,0};
	int array2[5] = {0,0,0,0,2};

	auto& a = array1;
	auto& b = array2;

	a[b[4]] = b[4];
	cout << a[2];

	return 0;
}
Would that be better in any way than the functions? I mostly just prefer the () versus [] (much easier to type on my keyboard).
Use a std::indirect_array<> along with std::valarray<> objects, perhaps?
http://www.cplusplus.com/reference/std/valarray/indirect_array/

Are you looking for operations of this kind?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <valarray>
#include <iostream>

int main()
{
    std::valarray<int> a = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120 } ;
    std::valarray<int> b = { 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 } ;

    std::valarray<std::size_t> i = { 1, 3, 5, 7, 9 } ;

    for( int v : a ) std::cout << v << ' ' ; std::cout << '\n' ;

    a[i] = 99 ;
    for( int v : a ) std::cout << v << ' ' ; std::cout << '\n' ;

    a[i] = b[i] ;
    for( int v : a ) std::cout << v << ' ' ; std::cout << '\n' ;

    a[i] += b[i] ;
    for( int v : a ) std::cout << v << ' ' ; std::cout << '\n' ;

}


Output:
10 20 30 40 50 60 70 80 90 100 110 120
10 99 30 99 50 99 70 99 90 99 110 120
10 16 30 18 50 20 70 22 90 24 110 120
10 32 30 36 50 40 70 44 90 48 110 120
Topic archived. No new replies allowed.