![](/img/trans.png)
[英]How do I find out the type of the GLuint id passed to glGetObjectLabel?
[英]How do I find out if a tuple contains a type?
假设我想从一些非唯一类型序列创建一个唯一类型的编译时异构容器。 为了做到这一点,我需要遍历源类型(某种tuple
)并检查每种类型是否已经存在于我的“唯一”元组中。
我的问题是:如何检查元组(或boost::fusion
容器)是否包含类型?
我愿意使用 STL 或boost
。
#include <tuple>
#include <type_traits>
template <typename T, typename Tuple>
struct has_type;
template <typename T>
struct has_type<T, std::tuple<>> : std::false_type {};
template <typename T, typename U, typename... Ts>
struct has_type<T, std::tuple<U, Ts...>> : has_type<T, std::tuple<Ts...>> {};
template <typename T, typename... Ts>
struct has_type<T, std::tuple<T, Ts...>> : std::true_type {};
还有一个额外的别名,如果特征本身应该是std::true_type
或std::false_type
:
template <typename T, typename Tuple>
using tuple_contains_type = typename has_type<T, Tuple>::type;
在 C++17 中,你可以这样做:
template <typename T, typename Tuple>
struct has_type;
template <typename T, typename... Us>
struct has_type<T, std::tuple<Us...>> : std::disjunction<std::is_same<T, Us>...> {};
在 C++11 中,您必须滚动自己的or
/ disjunction
。 这是一个完整的 C++11 版本,带有测试:
#include <tuple>
#include <type_traits>
template<typename... Conds>
struct or_ : std::false_type {};
template<typename Cond, typename... Conds>
struct or_<Cond, Conds...> : std::conditional<Cond::value, std::true_type, or_<Conds...>>::type
{};
/*
// C++17 version:
template<class... B>
using or_ = std::disjunction<B...>;
*/
template <typename T, typename Tuple>
struct has_type;
template <typename T, typename... Us>
struct has_type<T, std::tuple<Us...>> : or_<std::is_same<T, Us>...> {};
// Tests
static_assert(has_type<int, std::tuple<>>::value == false, "test");
static_assert(has_type<int, std::tuple<int>>::value == true, "test");
static_assert(has_type<int, std::tuple<float>>::value == false, "test");
static_assert(has_type<int, std::tuple<float, int>>::value == true, "test");
static_assert(has_type<int, std::tuple<int, float>>::value == true, "test");
static_assert(has_type<int, std::tuple<char, float, int>>::value == true, "test");
static_assert(has_type<int, std::tuple<char, float, bool>>::value == false, "test");
static_assert(has_type<const int, std::tuple<int>>::value == false, "test"); // we're using is_same so cv matters
static_assert(has_type<int, std::tuple<const int>>::value == false, "test"); // we're using is_same so cv matters
我实际上需要一个项目这样的东西。 这是我的解决方案:
#include <tuple>
#include <type_traits>
namespace detail {
struct null { };
}
template <typename T, typename Tuple>
struct tuple_contains;
template <typename T, typename... Ts>
struct tuple_contains<T, std::tuple<Ts...>> :
std::integral_constant<
bool,
!std::is_same<
std::tuple<typename std::conditional<std::is_same<T, Ts>::value, detail::null, Ts>::type...>,
std::tuple<Ts...>
>::value
>
{ };
这种方法的主要优点是它是一个实例化,不需要递归。
因为没有人发布它,我正在添加一个基于我在 SO 上学到的 bool 技巧的解决方案:
#include<type_traits>
#include<tuple>
template<bool...>
struct check {};
template<typename U, typename... T>
constexpr bool contains(std::tuple<T...>) {
return not std::is_same<
check<false, std::is_same<U, T>::value...>,
check<std::is_same<U, T>::value..., false>
>::value;
}
int main() {
static_assert(contains<int>(std::tuple<int, char, double>{}), "!");
static_assert(contains<char>(std::tuple<int, char, double>{}), "!");
static_assert(contains<double>(std::tuple<int, char, double>{}), "!");
static_assert(not contains<float>(std::tuple<int, char, double>{}), "!");
static_assert(not contains<void>(std::tuple<int, char, double>{}), "!");
}
就编译时性能而言,它比公认的解决方案慢,但值得一提。
在 C++14 中,编写起来会更容易。 标准模板已经在<utility>
标头中提供了您需要执行的所有操作:
template<typename U, typename... T>
constexpr auto contains(std::tuple<T...>) {
return not std::is_same<
std::integer_sequence<bool, false, std::is_same<U, T>::value...>,
std::integer_sequence<bool, std::is_same<U, T>::value..., false>
>::value;
}
这在概念上与std::get
所做的(从 C++14 开始可用于类型)相去甚远,但请注意,如果类型U
在T...
出现多次,后者将无法编译。
是否符合您的要求主要取决于实际问题。
这是一个不会递归实例化模板以检查匹配类型的版本。 相反,它使用 SFINAE 和基于索引的元编程:
#include <type_traits>
#include <tuple>
template <std::size_t... Indices>
struct index_sequence {
typedef index_sequence<Indices..., sizeof...(Indices)> next;
};
template <std::size_t Start>
struct make_index_sequence {
typedef typename make_index_sequence<Start - 1>::type::next type;
};
template <>
struct make_index_sequence<0> {
typedef index_sequence<> type;
};
template <int n>
using make_index_sequence_t = typename make_index_sequence<n>::type;
template <typename Value, typename Sequence>
struct lookup;
template <typename Value, std::size_t... index>
struct lookup<Value, index_sequence<index...>>
{
private:
struct null;
template <typename... Args>
static std::false_type
apply(std::conditional_t<std::is_convertible<Args, Value>::value, null, Args>...);
template <typename...>
static std::true_type apply(...);
template <typename... Args>
static auto apply_helper(Args&&...) ->
decltype(apply<std::remove_reference_t<Args>...>(std::declval<Args>()...));
public:
template <typename Tuple>
using value = decltype(
apply_helper(
std::declval<
typename std::tuple_element<index, Tuple>::type
>()...
)
);
};
template <typename Value, typename Tuple>
using has_type = decltype(
typename lookup<Value,
make_index_sequence_t<std::tuple_size<Tuple>::value>
>::template value<Tuple>{}
);
使用折叠表达式的C++17 及更高版本的解决方案:
template<typename U, typename... T>
constexpr bool contains(std::tuple<T...>) {
return (std::is_same_v<U, T> || ...);
}
既然你要求了,这里是一个boost::mpl
版本:
#include <boost/mpl/unique.hpp>
#include <boost/mpl/sort.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/type_traits/is_same.hpp>
using namespace boost;
template<typename Seq>
struct unique_concat :
mpl::unique<typename mpl::sort<Seq, is_same<mpl::_1,mpl::_2>>::type,
is_same<mpl::_1,mpl::_2>> {};
template<typename T>
struct print;
int main()
{
typedef mpl::vector<int, float, float, char, int, double, int> input;
print<unique_concat<input>::type> asdf;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.