英文:
SFINAE: Using template child of a template struct
问题
The proper way to do such things is as follows:
如何正确地执行这种操作?
I would like to use conditions on parametrs pack like:
我想在参数包上使用条件,例如:
static_assert(all_of<ChildTypes...>::are_in<ParentTypes...>::value);
static_assert(all_of<ChildTypes...>::are_in<ParentTypes...>::value);
In short I have code like that:
简而言之,我有以下代码:
template <class ... Types> struct is_in
{
template <class Arg>
struct test : any_of<Types...>::is_same_as<Arg> {}; // error C2059
};
template <class ... Args>
struct all_of
{
template <class ... Types>
struct are_in : all_fulfill<typename is_in<Types...>::test, Args...> {};
template <class Type>
struct are : all_fulfill<typename is_same_as<Type>::test, Args...> {};
};
where any_of<Types...>::is_same_as<Type>::value and all_of<Types...>::are<Type>::value work well when being used in functions so all_fulfill and any_fulfills also work well (always are true_type or false_type).
在函数中使用时,any_of<Types...>::is_same_as<Type>::value和all_of<Types...>::are<Type>::value都能很好地工作,因此all_fulfill和any_fulfills也能很好地工作(始终是true_type或false_type)。
When trying to call are_in I have following errors:
尝试调用are_in时,我遇到以下错误:
error C2059: syntax error: '<'
error C2039: 'value': is not a member of 'is_in<bool,int>::test<Head>' with [ Head=int ]
error C2065: 'value': undeclared identifier
error C2975: '_Test': invalid template argument for 'std::conditional', expected compile-time constant expression
错误C2059:语法错误:'<'
错误C2039:'value':不是'is_in<bool,int>::test<Head>'的成员,其中[ Head=int ]
错误C2065:'value':未声明的标识符
错误C2975:'_Test':'std::conditional'的无效模板参数,需要编译时常量表达式
As background:
背景信息:
template <template <class> typename Test, typename... List >
struct all_fulfill : std::false_type {};
template <template <class> typename Test, typename Head, typename... List >
struct all_fulfill<Test, Head, List ...>
: std::conditional< Test<Head>::value, // error C2065 // error C2975
all_fulfill<Test, List...>, // error C2039
std::false_type
>::type {};
template <template <class> typename Test >
struct all_fulfill<Test> : std::true_type {};
作为背景:
template <template <class> typename Test, typename... List >
struct all_fulfill : std::false_type {};
template <template <class> typename Test, typename Head, typename... List >
struct all_fulfill<Test, Head, List ...>
: std::conditional< Test<Head>::value, // error C2065 // error C2975
all_fulfill<Test, List...>, // error C2039
std::false_type
>::type {};
template <template <class> typename Test >
struct all_fulfill<Test> : std::true_type {};
英文:
What is the proper way of doing such things?
I would like to use conditions on parametrs pack like:
static_assert(all_of<ChildTypes ...>::are_in<ParentTypes ...>::value);
In short I have code like that:
template <class ... Types> struct is_in
{
template <class Arg>
struct test : any_of<Types ...>::is_same_as<Arg> {}; // error C2059
};
template <class ... Args>
struct all_of
{
template <class ... Types>
struct are_in : all_fulfill<typename is_in<Types ...>::test, Args ...> {};
template <class Type>
struct are : all_fulfill<typename is_same_as<Type>::test, Args ...> {};
};
where any_of<Types...>::is_same_as<Type>::value
and all_of<Types...>::are<Type>::value
work well when being used in functions so all_fulfill
and `any_fulfills' also work well (always are true_type or false_type).
When trying to call are_in I have following errors:
error C2059: syntax error: '<'
error C2039: 'value': is not a member of 'is_in<bool,int>::test<Head>' with [ Head=int ]
error C2065: 'value': undeclared identifier
error C2975: '_Test': invalid template argument for 'std::conditional', expected compile-time constant expression
As background:
template < template <class> typename Test, typename... List >
struct all_fulfill : std::false_type {};
template < template <class> typename Test, typename Head, typename... List >
struct all_fulfill<Test, Head, List ...>
: std::conditional< Test<Head>::value, // error C2065 // error C2975
all_fulfill<Test, List...>, // error C2039
std::false_type
>::type {};
template < template <class> typename Test >
struct all_fulfill<Test> : std::true_type {};
Tried:
In struct is_in
adding alias for parent type like:
using AnyOfTypes = any_of<Types ...>;
and struct AnyOfTypes : any_of<Types ...> {}
Expected result:
all_of<ChildTypes ...>::are_in<ParentTypes ...>::value
should return proper bool.
答案1
得分: 1
以下是代码的翻译部分:
#include <type_traits>
template <typename... Types>
struct all_of
{
template <typename Arg>
constexpr static bool contains = (std::is_same_v<Arg, Types> || ...);
template <typename... Args>
constexpr static bool are_in = (all_of<Args...>::template contains<Types> && ...);
};
int main()
{
static_assert(all_of<bool, int>::are_in<int, double, bool>);
return 0;
}
你提供的链接是一个代码示例,用C++编写,用于检查一组类型是否包含在另一组类型中。如果你需要进一步的信息或有其他问题,请随时提出。
英文:
How about this?
#include <type_traits>
template <typename... Types>
struct all_of
{
template <typename Arg>
constexpr static bool contains = (std::is_same_v<Arg, Types> || ...);
template <typename... Args>
constexpr static bool are_in = (all_of<Args...>::template contains<Types> && ...);
};
int main()
{
static_assert(all_of<bool, int>::are_in<int, double, bool>);
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论