C++ 那些被遗漏的细节3 std::get

C++11引入了std::tuple,提供了通过索引获取元素的方法。从C++14开始,std::get进一步支持通过元素类型来获取tuple中的元素。此特性增加了代码的灵活性,但需要注意,当类型不唯一时,使用类型获取元素会导致编译错误。示例代码展示了如何通过索引和类型获取tuple元素,以及在类型不唯一时的错误情况。
简述
  • 在之前的文章C++11 std::tuple 中有使用std::get获取std::tuple元素的例子。
  • std::get除了通过索引(C++11)获取std::tuple的元素,还能通过元素类型获取元素(C++14)
语法
// 头文件 <tuple>
template< std::size_t I, class... Types >
typename std::tuple_element<I, tuple<Types...> >::type&
    get( tuple<Types...>& t ) noexcept; (1)	(since C++11)
(constexpr since C++14)
template< std::size_t I, class... Types >
typename std::tuple_element<I, tuple<Types...> >::type&&
    get( tuple<Types...>&& t ) noexcept; (2)	(since C++11)
(constexpr since C++14)
template< std::size_t I, class... Types >
typename std::tuple_element<I, tuple<Types...> >::type const&
    get( const tuple<Types...>& t ) noexcept; (3)	(since C++11)
(constexpr since C++14)
template< std::size_t I, class... Types >
typename std::tuple_element<I, tuple<Types...> >::type const&&
    get( const tuple<Types...>&& t ) noexcept; (4)	(since C++11)
(constexpr since C++14)
template< class T, class... Types >
constexpr T& get(tuple<Types...>& t) noexcept; (5)	(since C++14)
template< class T, class... Types >
constexpr T&& get(tuple<Types...>&& t) noexcept; (6)	(since C++14)
template< class T, class... Types >
constexpr const T& get(const tuple<Types...>& t) noexcept; (7)	(since C++14)
template< class T, class... Types >
constexpr const T&& get(const tuple<Types...>&& t) noexcept; (8)	(since C++14)
  • 1-4通过索引获取元素,5-8通过元素类型获取元素,特别注意,如果该类型的元素不唯一,将获取失败。
例子
#include <iostream>
#include <tuple>
#include <string>

int main()
{
  std::tuple<int, int> t1{1, 1};
  std::cout << "t1 is {" << std::get<0>(t1) << ", " << std::get<1>(t1) << "}" << std::endl;

  std::tuple<int, std::string> t2{1, "hello world"};
  std::cout << "t2 is {" << std::get<int>(t2) << ", " << std::get<std::string>(t2) << "}" << std::endl;

  std::tuple<int, int> t3{1, 1};
  // error: static_assert failed due to requirement 'value != __ambiguous' 
  // "type occurs more than once in type list"
  // std::cout << "t3 is {" << std::get<int>(t3);
  return 0;
}
参考
--- stderr: card_drawing2 In file included from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13, from /usr/include/c++/11/algorithm:74, from /opt/ros/humble/include/rclcpp/rclcpp/executor.hpp:18, from /opt/ros/humble/include/rclcpp/rclcpp/executors/multi_threaded_executor.hpp:25, from /opt/ros/humble/include/rclcpp/rclcpp/executors.hpp:21, from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:155, from /home/f972/assi_ws/src/card_drawing2/src/card_server.cpp:1: /usr/include/c++/11/functional: In instantiation of ‘struct std::_Bind_check_arity<void (CardServer::*)(std::shared_ptr<card_interface::srv::String_Response_<std::allocator<void> > >), CardServer*, const std::_Placeholder<1>&, const std::_Placeholder<2>&>’: /usr/include/c++/11/functional:768:12: required from ‘struct std::_Bind_helper<false, void (CardServer::*)(std::shared_ptr<card_interface::srv::String_Response_<std::allocator<void> > >), CardServer*, const std::_Placeholder<1>&, const std::_Placeholder<2>&>’ /usr/include/c++/11/functional:789:5: required by substitution of ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (CardServer::*)(std::shared_ptr<card_interface::srv::String_Response_<std::allocator<void> > >); _BoundArgs = {CardServer*, const std::_Placeholder<1>&, const std::_Placeholder<2>&}]’ /home/f972/assi_ws/src/card_drawing2/src/card_server.cpp:13:38: required from here /usr/include/c++/11/functional:756:21: error: static assertion failed: Wrong number of arguments for pointer-to-member 755 | static_assert(_Varargs::value | ~~~~~ 756 | ? sizeof...(_BoundArgs) >= _Arity::value + 1 | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值