SFINAE: 使用模板结构的模板子类

huangapple go评论63阅读模式
英文:

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: '&lt;'
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:语法错误:'&lt;'
错误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&lt;ChildTypes ...&gt;::are_in&lt;ParentTypes ...&gt;::value);

In short I have code like that:

template &lt;class ... Types&gt; struct is_in
{
template &lt;class Arg&gt;
struct test : any_of&lt;Types ...&gt;::is_same_as&lt;Arg&gt; {}; // error C2059
};
template &lt;class ... Args&gt;
struct all_of
{
template &lt;class ... Types&gt;
struct are_in : all_fulfill&lt;typename is_in&lt;Types ...&gt;::test, Args ...&gt; {};
template &lt;class Type&gt;
struct are : all_fulfill&lt;typename is_same_as&lt;Type&gt;::test, Args ...&gt; {};
};

where any_of&lt;Types...&gt;::is_same_as&lt;Type&gt;::value and all_of&lt;Types...&gt;::are&lt;Type&gt;::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: &#39;&lt;&#39;
error C2039: &#39;value&#39;: is not a member of &#39;is_in&lt;bool,int&gt;::test&lt;Head&gt;&#39; with [ Head=int ]
error C2065: &#39;value&#39;: undeclared identifier
error C2975: &#39;_Test&#39;: invalid template argument for &#39;std::conditional&#39;, expected compile-time constant expression

As background:

template &lt; template &lt;class&gt; typename Test, typename... List &gt;
struct all_fulfill : std::false_type {};
template &lt; template &lt;class&gt; typename Test, typename Head, typename... List &gt;
struct all_fulfill&lt;Test, Head, List ...&gt;
: std::conditional&lt; Test&lt;Head&gt;::value, // error C2065 // error C2975
all_fulfill&lt;Test, List...&gt;, // error C2039
std::false_type
&gt;::type {};
template &lt; template &lt;class&gt; typename Test &gt;
struct all_fulfill&lt;Test&gt; : std::true_type {};

Tried:

In struct is_in adding alias for parent type like:
using AnyOfTypes = any_of&lt;Types ...&gt;; and struct AnyOfTypes : any_of&lt;Types ...&gt; {}

Expected result:

all_of&lt;ChildTypes ...&gt;::are_in&lt;ParentTypes ...&gt;::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 &lt;type_traits&gt;

template &lt;typename... Types&gt;
struct all_of
{
    template &lt;typename Arg&gt;
    constexpr static bool contains = (std::is_same_v&lt;Arg, Types&gt; || ...);

    template &lt;typename... Args&gt;
    constexpr static bool are_in = (all_of&lt;Args...&gt;::template contains&lt;Types&gt; &amp;&amp; ...);
};

int main()
{
    static_assert(all_of&lt;bool, int&gt;::are_in&lt;int, double, bool&gt;);
    return 0;
}

https://godbolt.org/z/Y1j7dedsh

huangapple
  • 本文由 发表于 2023年3月15日 19:15:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定