6

I have been given an exercise where I need to use a type alias for a 'reference to 3 integers'. Although I found success using typedef I'm not able to replicate it by using introduced by c++11.

Code :

typedef int (& int_ref)[3]; \\success

using int_ref2 = (int &) [3]; \\error

Should I then just use something like ...

using int_ref2 = int [3];

int_ref2 & iruvar ...

3
  • 4
    It's the same thing without the identifier: using int_ref2 = int (&)[3]
    – David G
    Dec 11, 2015 at 14:58
  • Just remember that every time you obscure your code like this it increases the difficulty to read and debug the code; especially for other people who have to maintain it. The larger the project the worse it becomes.
    – soulsabr
    Dec 11, 2015 at 15:01
  • IMO you should avoid things like references or pointers in type-aliases, it will make code harder to read and understand. Dec 11, 2015 at 15:01

1 Answer 1

6

Compare these two declarations

typedef int (& int_ref)[3]; \\success

using int_ref2 =  (int &) [3]; \\error 

As you can see there is a difference: in the second declaration type specifier int is inside the parentheses.

So place it outside the parentheses

using int_ref2 =  int( & )[3];

All you need is to move the type name used in the typedef outside the parentheses to the left side relative to the equation sign.

The advantage of using the using declaration is that it has more strict and clear style.

Compare it for example with the following typedef declaration

int typedef (& int_ref)[3];

that is also a valid declaration.:)

2

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.