What are the templates that I have to specialize to support std::get?
struct MyClass {
int a;
};
template
struct MyContainer {
MyClas
I'm guessing you want to implement some algorithms that need access to arbitrary array-like containers using compile-time indices and therefor aim to use some function (like std::get
) to uniformly perform that task?!
In that case, it is the same business as making begin
and end
available for your class. You simply declare a function get
in the namespace you declared your container class in, and let ADL do its jobs.
template <unsigned I, unsigned N>
MyClass& get (MyContainer<N>& c) { return c.array[I]; }
template <unsigned I, unsigned N>
MyClass const& get (MyContainer<N> const& c) { return c.array[I]; }
In your algorithm you just use get
(without the std
namespace prefix) and ADL will call the correct function. So, for the standard structures like array
, tuple
and pair
std::get
is invoked and for your container the get
function that you provided.
int main(){
std::array<int, 3> a {{0,1,2}};
auto t = std::make_tuple(0.0, 1.0f, 2);
auto p = std::make_pair('0', 4.4);
MyContainer<3> c;
std::cout << get<1>(a) << std::endl;
std::cout << get<1>(t) << std::endl;
std::cout << get<1>(p) << std::endl;
std::cout << get<1>(c).a << std::endl;
return 0;
}
Example
std::get
is not a customization point for the standard library; the three function template overloads (for pair
, tuple
and array
) do not explicitly allow for user-defined overloads, so 17.6.4.2.1p1 applies and adding a declaration of your own function template overload is undefined behaviour.
Note that get
as an unqualified name is a customization point as of C++17; it is used by the structured binding declaration protocol to access tuple-like elements; but this is as an unqualified name, not the qualified name std::get
.
That said, if you were to write:
namespace std {
template<size_t I, size_t N> MyClass &get(MyContainer<N> &c) { return c.array[I]; }
}
and similarly for the rvalue reference and const reference overloads, your program would likely work as you expect.
However, there's little point seeing as the standard already supplies array
:
template<size_t N> using MyContainer = std::array<MyClass, N>;