-2

It is easy to achieve this with typedef:

typedef int (*ptr_arr)[]; // pointer to array

But how to alias the same type as ptr_arr by using C++ using keyword?


Also would be nice to see (good formatted) syntax difference between aliasing: pointer to array vs array of pointers with both typedef and using.

2
  • It is strange to link this question about arrays to another question about functions. They might seem similar due to common pointer base in both topics. But the solution to each question has different: syntax and meaning. Also while initially searching for solution to this (array) question - no suitable answers were found on the net (not just stackoverflow), including that duplicate question (did not even appeared in my search results), which still would not answer my question back then, and not answering it right now. Commented Feb 15 at 10:43
  • Found a remotely similar question - but both question and the answer are robust and overcomplicated, making it hard to find the solution, especially with search engine. Commented Feb 16 at 14:04

3 Answers 3

6

It's a simple mechanical translation; you don't even need to understand what the alias means.

"ptr_arr" is the name, and everything else is the type, so just move the name to the left, and the "everything else" to the right:

using ptr_arr = int (*)[];

Sign up to request clarification or add additional context in comments.

3 Comments

You gave an interesting way to convert from typedef to using - "Just move typename to the left". Unfortunately this does not explain the crucial effect of () on resulting alias.
@intmain The question was about how to define a specific type alias with using, not about the significance of parentheses (which the question implies is already well understood).
Agree, the depicts is more suitable word than explains. It is easy to come to the int*[] alias (array of pointers), which is only () parentheses away from the final solution.
6

Using an extra level of abstraction removes the need to remember the complicated syntax.

If you add:

using int_array = int[];

You can then get a pointer to this type by simple adding a * like you normally do like:

using ptr_int_array = int_array*;

3 Comments

This solution is simple and valid! But: 1. While being similar to template method - it provides no quick IDE hints about underlying type. 2. Hint render namespace resolutions for both typenames which makes it even less readable. 3. Being splitted in 2 aliases. 4. Less generic than template method. Thank you!
@intmain If you name the type properly, you don't need to see the underlying type. This technique is valuable because it applies to many situations where building up a type by steps is more understandable.
@Rud48, sure every solution has its pros and cons. Just listed noticeable traits of this particular one, so it will be easier for future me and others to pick their most suitable solution.
3
using         ptr_arr_using = int(*)[];  // pointer to array <-- answer
typedef int (*ptr_arr_typedef)[];        // pointer to array
typedef int  *arr_ptr_typedef [];        // array   of pointers
using         arr_ptr_using = int * [];  // array   of pointers

#include <type_traits>
static_assert(std::is_same_v<ptr_arr_using, ptr_arr_typedef>); // pointer to array
static_assert(std::is_same_v<arr_ptr_using, arr_ptr_typedef>); // array of pointers

static_assert(!std::is_same_v<arr_ptr_using, ptr_arr_using>); // arr_ptr != ptr_arr

Important: () parentheses control operator precedence in declaration

What hinted me to this

An example "alias template" from https://en.cppreference.com/w/cpp/language/type_alias

template<class T> using ptr = T*;
using ptr_arr_using_template = ptr<int[]>; // then IDE showed hint: int(*)[]

Why it is so?

The dispatch order of operators in declaration is the same as in C++ Operator Precedence and Associativity table: https://en.cppreference.com/w/cpp/language/operator_precedence

TL;DR
  • [] - precede first and associated Left to right ->
  • & * * const/volatile - precede after and associated Right to left <-
  • () determine the highest precedence (for operators inside)

1 Comment

Credits: Thanks to Jerry Coffin, nwp for helping to shape the answer and wohlstad for reviewing the question in Staging Ground

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.