1

I have a class template

template <typename T>
struct Foo
{
  T value;

  // methods
  // ...
}

For the case where T is a std::array, I want to be able to call std::array::at on an instance of Foo directly. For example I want to be able to do

Foo<std::array<int, 5>> bar;
// ...
int x = bar.at(0);

However, currently I have to use bar.value.at(0).

Is it possible to define the function Foo::at only for the case where T is a std::array, which simply returns value.at()? I don't know how this is done since std::array itself has template arguments. Ideally I don't want to rewrite the entire template for the specialized case.

Thanks in advance.

thassan
  • 21
  • 3
  • Did you try something? Writing the method to work for `std::array`s is simple and straightforward. Not providing the method for all other `T`s is the tricky part – 463035818_is_not_an_ai Nov 29 '21 at 15:23
  • Add an `at` function to the class and use SFINAE to constrain it. You should be able to adapt [this](https://stackoverflow.com/questions/31762958/check-if-class-is-a-template-specialization) to tell you if `T` is a specialization of `std::array` – NathanOliver Nov 29 '21 at 15:27
  • Is it important that, say, `Foo> bar; bar.at(0);` **not** work? If it's OK for the method to work for both (and any other type that provides a suitable `at` method), then just implement `at` as if `T` is an `std::array`. – Igor Tandetnik Nov 29 '21 at 15:27

0 Answers0