我正在尝试编写迭代std::tuple元素的通用函数。这样做时,我需要提取正在处理的给定元组元素的元素类型。但是,我遇到了有关提取的类型和元组元素的实际类型之间的类型相等性的问题。下面的代码通过将 s 添加到提取的类型来说明问题static_assert,(据我所知)在这两种情况下都应该是正确的:
#include <tuple>
#include <type_traits>
using TestTuple = std::tuple<int>;
template <std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I == sizeof...(Tp), void>::type
foo(std::tuple<Tp...> &) {}
template <std::size_t I = 0, typename... Tp>
inline typename std::enable_if <
I<sizeof...(Tp), void>::type foo(std::tuple<Tp...> &output) {
// Using tuple element
using ValueType = std::tuple_element<I, std::tuple<Tp...>>;
static_assert(std::is_same<ValueType, int>::value, "Should be true");
using ValueType2 = std::remove_reference<decltype(std::get<I>(output))>;
static_assert(std::is_same<ValueType2, int>::value, "Should be true");
}
void bar() {
TestTuple t;
foo(t);
}
Run Code Online (Sandbox Code Playgroud)
尝试通过Compiler …