2

I have a templated class used for modelling views on objects, like std::shared_ptr and std::weak_ptr but without any owning semantics. The class internally holds a pointer to the viewed object and a functor which is called on class destruction (It is useful for reference counting the viewed object, or for thread-safe locking and releasing of the viewed resource).

Like the standard library counterparts, I would like my class to behave as expected when the owned object is an array (T[]). The problem I am facing comes from the fact that a pointer to an array of unknown bound is, by my understanding, illegal C++. More specifically, given that the template parameter of the class T is, say, int[], when in my class I write:

T& operator*() {
    return *internal_pointer;
}

I am in fact invoking undefined behaviour. (Or, possibly, some non-standard compiler extension?)

I am aware of the fact that a class template specialization is needed just to avoid these scenarios - a pointer to the element type int could be kept instead and just treated like a pointer to an array by the class. However, my question is this: why are pointers and references to arrays of unknown bound illegal?

Using them seems to me like the most logical thing to do, since what you're viewing is an array of which you might not necessarily know the length: this preserves the type of the viewed object, while a regular pointer to the element type seems to me nothing more than a hack.

Is there any technical reason to disallow references to arrays of unknown bound?

4
  • Everything has bounds. If you don't know the capacity of the container at compile time, use std::vector or std::list. Commented Jun 10, 2021 at 21:28
  • A thing that is not legal is to return an array from a function Commented Jun 10, 2021 at 21:50
  • A pointer or reference to an array of unknown bound is completely legal in C++ -- it just doesn't know its length. You can easily convert an int[6] into an int(*)[] or int(&)[], for example -- at least as of C++20. Commented Jun 10, 2021 at 22:36
  • Sorry if this is a late comment, but i struggle to find the correct way to do the conversion. I'm working in C++17 and when i do static_cast<int(*)[]> from type int(*)[2] the compiler complains. I tried to also cast the type through another indirection (cast the pointer to pointer to array) but that still doesn't help. I'm skeptical of using reinterpret_cast because there are no guarantees of the usability of the resulting pointer, and casting through void* seems like cheating. Do you know of a solution? Commented Jul 16, 2021 at 9:35

1 Answer 1

4

The problem I am facing comes from the fact that a pointer to an array of unknown bound is, by my understanding, illegal C++.

You're mistaken. Pointer to an array of unknown bound is not illegal in C++.

I am in fact invoking undefined behaviour. (Or, possibly, some non-standard compiler extension?)

Neither (as long as the pointer is valid). The shown function is standard conforming even if T is an array of unknown bound.

why are pointers and references to arrays of unknown bound illegal?

They aren't illegal.


There used to be a special case that pointers and references to arrays of unknown bound were illegal as function parameters. That was made legal in a defect resolution in 2014

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.