RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1394743
Accepted
OlegUP
OlegUP
Asked:2022-08-29 15:06:59 +0000 UTC2022-08-29 15:06:59 +0000 UTC 2022-08-29 15:06:59 +0000 UTC

C++ Typelist 和可变参数模板绑定元函数。编译时出错

  • 772

我Typelist用于分组类型。为此,我实现了一个元函数tlist_find_if,它接受一个谓词Pred和一个类型列表作为输入,如果条件匹配,则返回列表中包含的类型。

// template <typename... Ts>
struct tlist
{
   using type = tlist;
   static constexpr size_t size() noexcept { return sizeof...(Ts); }
};
    
template <template<typename T> typename Pred, typename TList>
struct tlist_find_if_impl;

template <template<typename T> typename Pred>
struct tlist_find_if_impl<Pred, tlist<>> { using result = null_t; };

template <template<typename T> typename Pred, typename H, typename... Ts>
struct tlist_find_if_impl<Pred, tlist<H, Ts...>>
{   
    using result = typename std::conditional<Pred<H>::value, H, typename tlist_find_if_impl<Pred, tlist<Ts...> >::result>::type;
};

// Wrapper
template <template <typename T> typename Pred, typename TList>
struct tlist_find_if;

template <template <typename T> typename Pred, typename... Ts>
struct tlist_find_if<Pred, tlist<Ts...>>
{   
    using result = typename tlist_find_if_impl<Pred, tlist<Ts...> >::result;
};

我还实现了一个元函数bind来组合谓词:

template <template <typename...> typename F, typename T>
struct bind
{
    template <typename... Ts> 
    using type = F<T, Ts...>;
};

但是在使用它时,编译器要么接受所有内容,要么给出错误。

没有错误的示例:

int main()
{
    // ...
    using type = tlist_find_if<bind<std::is_same, a1_t>::type, x_group >::result; // , x_group>;
    static_assert(std::is_same_v<a1_t, type>);
}

与特定类型一起使用时没有错误。但是在模板元函数中使用时,编译器会抛出错误。

    template <typename Group>
    struct type_in_group_pred
    {            
        static constexpr bool value = std::is_same<Group, typename tlist_find_if<bind<std::is_same, T>::type, Group>::result>::value; // Ошибка!
    };
    using group = typename tlist_find_if<type_in_group_pred, GroupList>::result;

错误文字:

main2.cc:107:117: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class T> class Pred, class TList> struct tlist_find_if’
  107 |         static constexpr bool value = std::is_same<Group, typename tlist_find_if<bind<type_in_group, T>::type, Group>::result>::value;
      |                                                                                                                     ^
main2.cc:107:116: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class T> class Pred, class TList> struct tlist_find_if’
  107 |         static constexpr bool value = std::is_same<Group, typename tlist_find_if<bind<std::is_same, T>::type, Group>::result>::value;
      |                                                                                                                    ^
main2.cc:107:116: note:   expected a class template, got ‘bind<std::is_same, T>::type’

也就是说,由于某种原因,编译器将 bind 的结果读取为具体类型,而不是模板。但为什么?在没有错误的示例中,一切正常。

供复制的样本测试程序的全文

#include <type_traits>
#include <stdio.h>:

struct a1_t { int x; };
struct a2_t { int x; };
struct b1_t { int y; };
struct b2_t { int y; };
struct c1_t { int z; };
struct c2_t { int z; };
struct null_t {};


template <typename... Ts>
struct tlist
{
   using type = tlist;
   static constexpr size_t size() noexcept { return sizeof...(Ts); }
};

template <template<typename T> typename Pred, typename TList>
struct tlist_find_if_impl; // { using result = null_t; };

template <template<typename T> typename Pred>
struct tlist_find_if_impl<Pred, tlist<>> { using result = null_t; };

template <template<typename T> typename Pred, typename H, typename... Ts>
struct tlist_find_if_impl<Pred, tlist<H, Ts...>>
{
    using result = typename std::conditional<Pred<H>::value, H, typename tlist_find_if_impl<Pred, tlist<Ts...> >::result>::type;
};

// Wrapper
template <template <typename T> typename Pred, typename TList>
struct tlist_find_if;

template <template <typename T> typename Pred, typename... Ts>
struct tlist_find_if<Pred, tlist<Ts...>>
{
    using result = typename tlist_find_if_impl<Pred, tlist<Ts...> >::result;
};

template <typename T, typename U>
struct null_t_wrap
{
    using type = typename std::conditional<std::is_same_v<null_t, T>, U, T>::type;
};

template <template <typename...> typename F, typename T>
struct bind
{
    template <typename... Ts>
    using type = F<T, Ts...>;
};

using null_group = tlist<>;
using x_group = tlist<a1_t, a2_t>;
using y_group = tlist<b1_t, b2_t>;
using group_tlist = tlist<x_group, y_group>;

template <typename T>
struct null_setter { static void set(T& t, int x) { (void)t; (void)x; printf("null setter x = %d\n", x); } };

template <typename T>
struct x_setter { static void set(T& t, int x) { t.x = x; printf("set x = %d\n", x); } };

template <typename T>
struct y_setter { static void set(T& t, int y) { t.y = y; printf("set y = %d\n", y); } };

template <typename T, typename Group>
struct group_2_setter { using setter = null_setter<T>;  };
template <typename T>
struct group_2_setter<T, x_group> { using setter = x_setter<T>; };
template <typename T>
struct group_2_setter<T, y_group> { using setter = y_setter<T>; };

template <typename T, typename Group>
struct type_2_group 
{ 
    template <typename H>
    struct type_is_same_pred
    {
        static constexpr bool value = std::is_same<T, H>::value;
    };
    // using group = typename null_t_wrap<typename tlist_find_if<type_is_same_pred, Group>::result, null_group>::type;                       
    using group = typename tlist_find_if<type_is_same_pred, Group>::result;                       
};

template <typename T, typename Group>
struct type_in_group
{
    template <typename U>
    struct is_t
    {
        static constexpr bool value = std::is_same_v<T, U>; 
    };
    static constexpr bool value = std::is_same<T, typename tlist_find_if<is_t, Group>::result>::value;
};

template <typename T, typename GroupList>
struct group_list_2_group 
{ 
    template <typename Group>
    struct type_in_group_pred
    {
        // static constexpr bool value = std::is_same<Group, typename type_2_group<T, Group>::group >::value;
        
        static constexpr bool value = std::is_same<Group, typename tlist_find_if<bind<std::is_same, T>::type, Group>::result>::value;
    };
   // using group = typename null_t_wrap<typename tlist_find_if<type_in_group_pred, GroupList>::result, null_group>::type;
//    using group = typename tlist_find_if<type_in_group_pred, GroupList>::result;
    using group = typename tlist_find_if<type_in_group_pred, GroupList>::result;
};

template <typename T, typename GroupList>
struct type_2_setter 
{
    using setter = typename group_2_setter<T, typename group_list_2_group<T, GroupList>::group>::setter; 
};

int main()
{
    a1_t a1; 
    a2_t a2;
    b1_t b1;
    b2_t b2;
    c1_t c1;
    c2_t c2;

    using is_same_as_a1_t = bind<std::is_same, a1_t>::type<a1_t>;
    static_assert(is_same_as_a1_t::value);
//    using a1_t_setter = type_2_setter<a1_t, group_tlist>::setter;
//    a1_t_setter::set(a1, 1);
//  using group = type_2_group<a1_t, x_group>;
//  printf("%s\n", typeid(group).name());
//  static_assert(std::is_same_v<group, null_t>);
    
//    using a2_t_setter = type_2_setter<a2_t, group_tlist>::setter;
//    a2_t_setter::set(a2, 2);
        
//    using b1_t_setter = type_2_setter<b1_t, group_tlist>::setter;
//    b1_t_setter::set(b1, 1);
    
    using b2_t_setter = type_2_setter<b2_t, group_tlist>::setter;
    b2_t_setter::set(b2, 2);

//    using c1_t_setter = type_2_setter<c1_t, group_tlist>::setter;
//    c1_t_setter::set(c1, 2);

    using type = tlist_find_if<bind<std::is_same, a1_t>::type, x_group >::result; // , x_group>;
    static_assert(std::is_same_v<a1_t, type>);
    static_assert(std::is_same_v<a1_t, tlist_find_if<bind<std::is_same, a1_t>::type, x_group>::result>);
}
c++
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    OlegUP
    2022-08-29T20:10:08Z2022-08-29T20:10:08Z

    正确的解决方法是使用关键字templatebeforetype

    template <typename T, typename GroupList>
    struct group_list_2_group 
    { 
        template <typename Group>
        struct type_in_group_pred
        {   
            static constexpr bool value = std::is_same<Group, typename tlist_find_if<bind<std::is_same, T>::template type, Group>::result>::value;
        };  
    
        using group = typename tlist_find_if<type_in_group, GroupList>::result;
    };
    
    • 0

相关问题

  • 编译器和模板处理

  • 指针。找到最小数量

  • C++,关于枚举类对象初始化的问题

  • 函数中的二维数组

  • 无法使用默认构造函数创建类对象

  • C++ 和循环依赖

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5