我要专门支持std :: get的模板是什么?
What are the templates that I have to specialize to support std::get?
struct MyClass { int a; }; template <const size_t I> struct MyContainer { MyClass array[I]; };我必须专门做什么才能做到:
What do I have to specialize to be able to do:
MyContainer<16> mc; std::get<0>(mc);推荐答案
std :: get 不是标准库的自定义点;三个函数模板重载( pair , tuple 和数组)不明确允许用户定义的重载,因此17.6.4.2.1p1适用并添加一个自己的函数模板重载的声明是未定义的行为。
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.
,如果您 写:
namespace std { template<size_t I, size_t N> MyClass &get(MyContainer<N> &c) { return c.array[I]; } }同样,对于右值引用和const引用重载,
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>;更多推荐
在类上启用std :: get支持
发布评论