简体   繁体   English

如何获取指向std :: get &lt;0,tuple的指针 <int,int> &gt;)?

[英]How to get a pointer to std::get<0,tuple<int,int>>)?

I know that std::get is overloaded. 我知道std :: get重载了。 And I know that to extract an overload I need to cast to a specific signature. 而且我知道要提取过载,我需要转换为特定的签名。 Let say I need a pointer to std::get which returns non-const ref to 1st element from std::tuple&. 假设我需要一个指向std :: get的指针,该指针将非常量ref从std :: tuple&返回到第一元素。 Below is one of my many attempts (does not compile): 以下是我的许多尝试之一(无法编译):

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

How should I specify this static_cast? 我应该如何指定此static_cast?

The signature of the tuple get is (directly taken from libstdc++): 元组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

As such the template parameter to get is the different types of the tuple, not the tuple, so the function you take the address from should be: 由于要获取的模板参数是元组的不同类型,而不是元组,因此从中获取地址的函数应为:

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

Do you need a pointer to an actual std::get or any function with the same behavior will do? 您是否需要一个指向实际std :: get的指针,或者具有相同行为的任何函数都可以呢? If so you can just wrap it in a lambda: 如果是这样,您可以将其包装在lambda中:

auto f = [](std::tuple<int,int>& tuple) -> int&
{
    return std::get<0>(tuple);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c++ get std::vector<int> 来自 std::vector <std::tuple<int, float> &gt; - c++ get std::vector<int> from std::vector<std::tuple<int, float>> 如何获取指针的int值 - how to get int value for pointer 有没有办法将 std::get(std::tuple) 与非常量 int 作为模板参数一起使用? - Is there a way to use std::get(std::tuple) with a non-const int as template argument? 为什么基于 int 的访问不适用于 std::get(std::tuple)? - Why doesn't the int-based access work for std::get(std::tuple)? std::less<int> ()(int, int) 可能会得到错误的答案</int> - The std::less<int>()(int, int) might get a wrong answer 如何从一行中的int引用获取指向const int的const指针的指针? - How to get pointer to const pointer to const int from int reference in one line? 通过将std :: tuple <int,int>赋值给const std :: tuple <int,int>&来延长它的生命周期 - Extending lifetime of std::tuple<int&,int> by assigning it to const std::tuple<int, int>& 如何将std :: vector的大小作为int? - How can I get the size of an std::vector as an int? 将指针传递给 int*&amp; 并获得编译错误 - Pass pointer to int*& and get compiled error 有没有可能传递std :: vector <int> 作为获取std :: vector的乐趣的参数 <std::array<int, 3> &gt;? - Is it possible without copy to pass std::vector<int> as param to fun that get std::vector<std::array<int, 3>>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM