获取满足std :: tuple特质的第一个元素

时间:2018-11-11 16:02:37

标签: c++ c++17 variadic-templates template-meta-programming stdtuple

我正在使用C ++ 17。我想得到一个满足某些类型特征的元组元素。如果可以通用地提供特性,那将是惊人的,但是我会对某个特性的特定功能感到满意。用法可能看起来像这样:

auto my_tuple = std::make_tuple { 0.f, 1 };

auto basic = get_if_integral (my_tuple);
auto fancy = get_if<std::is_floating_point> (my_tuple);

std::cout << basic; // '1'
std::cout << fancy; // '0.f'

理想情况下,如果多个元素满足特征,例如std::get (std::tuple),则将无法编译。

3 个答案:

答案 0 :(得分:1)

如果我正确理解了您想要的...,我提出如下的帮助器结构gf_h(“获得第一位帮助者”)

template <std::size_t, bool ...>
struct gf_h
 { };

template <std::size_t I, bool ... Bs>
struct gf_h<I, false, Bs...> : public gf_h<I+1u, Bs...>
 { };

template <std::size_t I, bool ... Bs>
struct gf_h<I, true, Bs...> : public std::integral_constant<std::size_t, I>
 { };

和一些使用它的函数:

template <typename ... Us,
   std::size_t I = gf_h<0, std::is_integral<Us>::value...>::value>
auto get_first_integral (std::tuple<Us...> const & t)
 { return std::get<I>(t); }

template <typename ... Us,
   std::size_t I = gf_h<0, std::is_floating_point<Us>::value...>::value>
auto get_first_floating (std::tuple<Us...> const & t)
 { return std::get<I>(t); }

观察到SFINAE启用/禁用的功能,因此只有在元组中存在整数(或浮点)值时才启用

以下是完整的编译示例

#include <tuple>
#include <iostream>

template <std::size_t, bool ...>
struct gf_h
 { };

template <std::size_t I, bool ... Bs>
struct gf_h<I, false, Bs...> : public gf_h<I+1u, Bs...>
 { };

template <std::size_t I, bool ... Bs>
struct gf_h<I, true, Bs...> : public std::integral_constant<std::size_t, I>
 { };

template <typename ... Us,
   std::size_t I = gf_h<0, std::is_integral<Us>::value...>::value>
auto get_first_integral (std::tuple<Us...> const & t)
 { return std::get<I>(t); }

template <typename ... Us,
   std::size_t I = gf_h<0, std::is_floating_point<Us>::value...>::value>
auto get_first_floating (std::tuple<Us...> const & t)
 { return std::get<I>(t); }

int main()
 {
   auto tup1 = std::make_tuple(3.f, 2., 1, 0);

   std::cout << get_first_integral(tup1) << std::endl; // 1
   std::cout << get_first_floating(tup1) << std::endl; // 3

   auto tup2 = std::make_tuple("abc", 4, 5);

   std::cout << get_first_integral(tup2) << std::endl; // 4
   // std::cout << get_first_floating(tup2) << std::endl; // error

   auto tup3 = std::make_tuple("xyz", 6., 7.f);

   // std::cout << get_first_integral(tup3) << std::endl; // error
   std::cout << get_first_floating(tup3) << std::endl; // 6
 }

答案 1 :(得分:1)

这是一种不使用递归的简单方法:

<!DOCTYPE html>
<html>
<head>
	<title>bootstrap</title>

	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

	<link rel="stylesheet" type="text/css" href="style.css">

	<link href="https://fonts.googleapis.com/css?family=Lora:700" rel="stylesheet">
</head>
<body>

	<nav class="navbar navbar-dark bg-dark">
	  <a class="navbar-brand" href="#">Landing page</a>
	  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
	    <span class="navbar-toggler-icon"></span>
	  </button>

	  <div class="collapse navbar-collapse" id="navbarSupportedContent">
	    <ul class="navbar-nav mr-auto">
	      <li class="nav-item active">
	        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
	      </li>
	      <li class="nav-item">
	        <a class="nav-link" href="#">Link</a>
	      </li>
	      <li class="nav-item dropdown">
	        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
	          Dropdown
	        </a>
	        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
	          <a class="dropdown-item" href="#">Action</a>
	          <a class="dropdown-item" href="#">Another action</a>
	          <div class="dropdown-divider"></div>
	          <a class="dropdown-item" href="#">Something else here</a>
	        </div>
	      </li>
	      <li class="nav-item">
	        <a class="nav-link disabled" href="#">Disabled</a>
	      </li>
	    </ul>
	    <form class="form-inline my-2 my-lg-0">
	      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
	      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
	    </form>
	  </div>
	</nav>


	<div class="container">
		<h1 id="title">The wait is over.</h1>
			<button type="button" class="btn btn-light">Find out more!</button>
	</div>

	<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
	<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>

可以很容易地扩展它以对特征进行参数化。

唯一使之成为C ++ 17的是template <template <typename...> typename T, typename... Ts> constexpr int index_of_integral(const T<Ts...>&) { const bool a[] = { std::is_integral_v<Ts>... }; for (int i = 0; i < sizeof...(Ts); ++i) if (a[i]) return i; return -1; } template <typename T> constexpr decltype(auto) get_if_integral(T&& t) { return std::get<index_of_integral(t)>(std::forward<T>(t)); } int main() { constexpr auto t = std::make_tuple(3.14, 42, "xyzzy"); static_assert(get_if_integral(t) == 42); } 变量模板和单参数is_integral_v。其他一切都是C ++ 14。

请注意,在C ++ 20中,static_assert循环可以替换为forstd::find

理想情况下,它应该引发异常而不是返回-1,但是编译器似乎不喜欢这样。

this answer的启发。

答案 2 :(得分:0)

好吧,我想出了一种方法来完成此任务,该方法不是特质上通用的,但这对于我当前的目的已经足够了。使用if constexpr看起来确实不错。我确定这不是很惯用,但是对我有用:

template <std::size_t Idx, typename... Us>
auto& get_if_integral_impl (std::tuple<Us...>& t)
{
    static_assert (Idx < std::tuple_size_v<std::tuple<Us...>>,
                   "No integral elements in this tuple.");

    if constexpr (std::is_integral<std::tuple_element_t<Idx, std::tuple<Us...>>>::value)
        return std::get<Idx> (t);
    else
        return get_if_integral_impl<Idx + 1> (t);
}

template<typename... Us>
auto& get_if_integral (std::tuple<Us...>& t)
{
    return get_if_integral_impl<0> (t);
}

auto tup = std::make_tuple (3.f, 2., 1, 0);
std::cout << get_if_integral (tup); // '1'

我的用例稍微复杂一点,涉及返回本身包含另一种类型的第一个嵌套元组,但这应该传达基本思想。