在类上启用std :: get支持

编程入门 行业动态 更新时间:2024-10-28 04:19:51
本文介绍了在类上启用std :: get支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要专门支持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支持

本文发布于:2023-10-13 09:27:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1487569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:std

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!