2

There's this homework question i've been wrecking my brain about:

I have to create an array class in C++ in which an index access to an element in the array is checked in compile-time, i.e. if I try to access the array with an index outside ita size it will cause a compilation error.

I thought I'd use enums as indexes instead of ints, but I spoke with my instructor and he told me that it's not the correct way, he also said that "think that for the same price you could use this to have an array where the indexes don't start at 0" or something like that.

I'd appreciate any suggestions!

1
  • 5
    You can not possibly report such error in places where the indexing is done using a variable, because most probably its value will only be known during runtime. Commented Jun 25, 2013 at 12:08

4 Answers 4

8

std::array from C++11 is just what you ask. It's an array with compile-time known size, which allows compile time checking out-of-bounds errors

Example:

std::array<int, 5> arr = {1, 2, 3, 4, 5};
std::get<3>(arr); // returns 4;
std::get<9>(arr); // COMPILE ERROR

Internally this array is implemented using templated array size (as you see from example, the second template argument in first line) and static_assert which performs compile-time checking for your condition (in this case it would be index < array_size). Also as you see in example you are using std::get instead of operator[], because, again, it uses templated argument as index, which has to be a constant expression (constexpr) to allow compile-time checking instead of run-time.

If you need a variable index, you can use old good operator[] but you won't have compile-time out-of-bounds checking which is clearly impossible to do at all.

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

4 Comments

Nice, but doesn't really explain how to create such class.
I added a little bit of explanation
thanks! but i'm pretty sure i can't use static_assert since it does pretty much exactly what i'm expected to do on my own
@dog Implementing your own static_assert is a whole different story alltogheter, requiring a bit of TMP fiddling, at least.
5

Here is a hint: If you want to check values at compile time, you essentially only have one option: you need to use non-type template arguments.

The standard library type std::tuple implements his, so check it out for inspiration on how to solve this.

5 Comments

Generally speaking your first sentence is not true - you can also use static_assert with constexpr values.
@sasha.sochka Sure. I wrote that in the context of calling a function at runtime and passing parameters into it. constexpr would work if it were allowed on function arguments.
This is allowed. Simple example: constexpr int sqr(int val) { return val * val; } int arr[sqr(3)];
@sasha.sochka Too hasty again. Of course that works. But sqr couldn’t be a member function operating on a non-constexpr object.
Ehhm, perhaps that doesn't really make sense anymore in the context of the original question but you can make sqr a member function operating on a non-constexpr object (returning non-constexpr obviously)
1

A hint, maybe phrased a bit differently: if you want to check the index at compile time, its value must also be known at compile time. Now, how to pass something to, say, a function at compile time?

To signal an error at compile time, static_assert can be used rather easily.

Comments

0

You might have a chance to use a custom class as the index type. You can then limit the generation of an index element by limiting the operators of the class. Especially this can ensure that no user input can be used as an index (if you don't provide any method to convert an integer (or similar) to the index type. If you allow this, you have no chance at all!).

2 Comments

i think this is what he meant, but i don't know how to make a class that has limited generations (a number which somehow the compiler knows about? ) this is a quote from a lecture slide: "Array references: it is impossible to detect overflow of array indices. Impossible when indices are of type integer – not if the type of the indices corresponds to the array size (which must be part of the type of the array)."
The quote from your lecture slides leads me to the assumption, that a template-driven solution might be expected (as @sasha_sochka noted).

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.