gpt4 book ai didi

c++ - 如何获取指向 std::get<0,tuple> 的指针?

转载 作者:太空狗 更新时间:2023-10-29 19:53:41 24 4
gpt4 key购买 nike

我知道 std::get 重载了。而且我知道要提取重载,我需要转换为特定的签名。假设我需要一个指向 std::get 的指针,它将非常量引用返回到 std::tuple& 中的第一个元素。下面是我的许多尝试之一(不编译):

auto f = static_cast<
int& (*)(std::tuple<int,int>&) noexcept
>(
&std::get<(size_t)0u, std::tuple<int,int>>
);

我应该如何指定这个 static_cast?

最佳答案

元组get的签名是(直接取自libstdc++):

 template<std::size_t __i, typename... _Elements>
constexpr typename __add_ref<
typename tuple_element<__i, tuple<_Elements...>>::type
>::type
get(tuple<_Elements...>& __t) noexcept

因此要获取的模板参数是元组的不同类型,而不是元组,因此您从中获取地址的函数应该是:

&std::get<(size_t)0u,int,int> 

关于c++ - 如何获取指向 std::get<0,tuple<int,int>> 的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11669630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com