C++: Return type of std::tie with std::ignore

我们两清 提交于 2019-12-23 09:55:51

问题


I am wondering if the C++11 standard gives any requirement about the type of the std::tuple returned by std::tie when some arguments are std::ignore.

More specifically, can I assume that:

  1. decltype(std::tie(42, std::ignore)) is not the same as decltype(std::tie(std::ignore, 42))
  2. decltype(std::tie(42, std::ignore)) is not the same as decltype(std::tie(42))
  3. decltype(std::tie(std::ignore, 42)) is not the same as decltype(std::tie(42))
  4. decltype(std::tie(std::ignore, std::ignore)) is not the same as decltype(std::tie(std::ignore))

In other words, from the type perspective, does the generated tuple behaves as a tuple which has type decltype(std::ignore) for all template arguments that match std::ignore by position?


回答1:


Yes you can, std::tie returns a std::tuple<T&...> where T... are the types that are given to it.
std::ignore has an unspecified type, but it will still appear in the tuple according to where you specified it in std::tie.

If if makes you feel better, you could include in your code somewhere:

    int n;
    auto i = std::tie(std::ignore, n);
    auto j = std::tie(n, std::ignore);
    auto k = std::tie(n);
    static_assert(!std::is_same<decltype(i), decltype(j)>::value, "");
    static_assert(!std::is_same<decltype(i), decltype(k)>::value, "");
    static_assert(!std::is_same<decltype(j), decltype(k)>::value, "");

and so on for whatever combinations you are explicitly using. This way compilation will fail if your assumption is invalid.



来源:https://stackoverflow.com/questions/22994074/c-return-type-of-stdtie-with-stdignore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!