我得到了一个简单的可变参数模板声明,就像经典的一样:
template <typename... Arguments>
class VariadicTemplate;
我需要实现的是让VariadicTemplate
类执行某些类型检查;可变参数模板应以某种迭代形式检查收到的所有参数应该说明类型<Foo>
。
我在某个地方看到了类似的东西,但现在我无法辨认它在哪里:P
答案 0 :(得分:6)
struct Foo {};
#include <type_traits>
template<class T, class...>
struct are_same : std::true_type
{};
template<class T, class U, class... TT>
struct are_same<T, U, TT...>
: std::integral_constant<bool, std::is_same<T,U>{} && are_same<T, TT...>{}>
{};
template<typename... Arguments>
class VariadicTemplate
{
static_assert(are_same<Foo, Arguments...>{}, "a meaningful error message");
};
int main()
{
VariadicTemplate<Foo, Foo, Foo, Foo> v0{}; (void)v0;
VariadicTemplate<Foo, int, Foo, double> v1{}; (void)v1;
}
但是有些东西告诉我你想知道参数是否是类模板Foo
的所有特化:
template<class T, class U>
struct Foo {};
#include <type_traits>
template<template<class...> class T, class U>
struct is_template_of
{
template<class... TT>
static std::true_type test(T<TT...>*);
static std::false_type test(...);
constexpr static bool value = decltype(test((U*)nullptr)){};
};
template<template<class...> class T, class...>
struct is_template_of_N : std::true_type
{};
template<template<class...> class T, class U, class... TT>
struct is_template_of_N<T, U, TT...>
: std::integral_constant<bool, is_template_of<T,U>::value
&& is_template_of_N<T, TT...>{}>
{};
template<typename... Arguments>
class VariadicTemplate
{
static_assert(is_template_of_N<Foo, Arguments...>{},
"a meaningful error message");
};
int main()
{
VariadicTemplate<Foo<int, double>, Foo<int, int>> v0; (void)v0;
VariadicTemplate<Foo<int, double>, int> v1; (void)v1;
}
答案 1 :(得分:2)
这里有另一种解决方案:P
这是:
template <bool... b> struct static_all_of;
// do recursion if the first argument is true
template <bool... tail>
struct static_all_of<true, tail...> : static_all_of<tail...> {};
// end recursion if first argument is false
template <bool... tail>
struct static_all_of<false, tail...> : std::false_type {};
// end recursion if no more arguments need to be processed
template <> struct static_all_of<> : std::true_type {};
// First template argument is given as the type checking for the is_base_of() function
template <typename Type, typename... Requirements>
class CollectionOfCommonBase : public Requirements...
{
static_assert(static_all_of<std::is_base_of<Type, Requirements>::value...>::value, "One or more template arguments are not base_of the one specified - given template specialization is not allowed.");
};
所以你得到它的工作:
class Foo {};
class AFoo : public Foo {};
class BFoo : public Foo {};
class MyCollection : public CollectionOfCommonBase<Foo, AFoo, BFoo> {};
答案 2 :(得分:0)
如果要检查类型(不询问基本类型):
template <typename ... Foos>
void Foo (Foos ... foos)
{
static_assert(are_same_v<int, Foos...>);
}
处理示例问题:
class _MytimeoffState extends State<Mytimeoff> {
List<Map> list = [];
void getList() async {
var data = await http
.get('https://my-json-server.typicode.com/darshanaAlex/db2/time_off');
var jsonData = json.decode(data.body);
setState(() {
for (Map js in jsonData) {
list.add(js);
}
});
print(jsonData);
}
@override
void initState() {
super.initState();
getList();
}
@override
Widget build(BuildContext context) {
return Scaffold();
}
}