How to find extent of a native array inserted in a unique_ptr?

Hi,

How can I find out the extent of the native array in the following code:

std::unique_ptr<std::string[]> up{ new std::string[10] };

I tried using:

auto up_extent = std::extent<decltype(up)>::value;

but it returns 0. I was expecting 10 as a result???

-----------
Well, after a bit of thought and experimentation, I found the following:

1
2
3
4
5
6
7
const int ints[] = { 1,2,3 };
		auto vInts = std::extent<decltype(ints)>::value;
		assert(vInts == 3);

		const int* pints = ints;
		auto vpints = std::extent<decltype(pints)>::value;
		assert(vpints == 0);


I know, pints is declared as a pointer to const int, while ints is declared as an array of const int. But shouldn't the compiler be smarter? It knows that pints was assigned with the value ints... why doesn't it use this information - instead of the static type info?



Thanks,
Juan
Last edited on
How can I find out the extent of the native array in the following code:

By keeping track of it yourself, or using a type (like std::vector, for instance) that does it for you.


I know, pints is declared as a pointer to const int, while ints is declared as an array of const int. But shouldn't the compiler be smarter? It knows that pints was assigned with the value ints... why doesn't it use this information - instead of the static type info?

In general, "this information" is not always available to the compiler. Should it behave one way when it is and another when it isn't? Do you really want your compiler guessing what you want instead of behaving deterministically?
Good point cire

Thanks !
Topic archived. No new replies allowed.