学习 C++20 concept/requires 中,元编程时遇到一个问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
zeal7s
V2EX    程序员

学习 C++20 concept/requires 中,元编程时遇到一个问题

 
  •   zeal7s 2022-01-08 11:47:55 +08:00 2388 次点击
    这是一个创建于 1403 天前的主题,其中的信息可能已经有所发展或是发生改变。

    新手,刚开始学习 C++模板元编程

    需求: 想实现这样一个简单的函数调用,对一个容器中的所有整数求和:

    sum<IntList<1, 2, 3>>(); 

    由于 C++标准中不允许函数模板来实现偏特化,所以我想用 concept/requires 来达到偏特化的需求。 我的实现如下:

    #include <iostream> template<int...N> class IntList; template<int...N> concept IsIntList = IntList<N...>{}; template<typename T> int sum() { return 0; } template<int...N> requires IsIntList<N...> int sum() { return (N + ...); } int main() { std::cout << sum<IntList<1, 2>>() << std::endl; return 0; } 

    实际运行时并不能达到我想要的结果,实际在C++ Insights中输出如下:

    #include <iostream> template<int...N> class IntList; template<int...N> concept IsIntList = IntList<N...>{}; template<typename T> int sum() { return 0; } /* First instantiated from: insights.cpp:21 */ #ifdef INSIGHTS_USE_TEMPLATE template<> int sum<IntList<1, 2> >() { return 0; } #endif template<int...N> requires IsIntList<N...> int sum() { return (N + ...); } int main() { std::cout.operator<<(sum<IntList<1, 2> >()).operator<<(std::endl); return 0; } 

    可以看出模板实例化时并没有对第二个sum进行实例化,而是对第一个sum实例化。

    请问各位大佬,在这种情况下,如何使用 concept/requires 来正确的实现我的需求?

    5 条回复    2022-01-08 22:14:22 +08:00
    ink19
        1
    ink19  
       2022-01-08 14:50:28 +08:00
    没有接触过 concept/requires 但是学过一些元编程,你这个想法可以用下面这个方法实现

    ```C++
    template<int ...N>
    class IntList{};

    template<typename T>
    class sum_impl;

    template<typename T>
    int sum() {
    return sum_impl<T>::value;
    }

    template<>
    class sum_impl<IntList<>> {
    public:
    const static int value = 0;
    };

    template<int first, int ...other>
    class sum_impl<IntList<first, other...>> {
    public:
    const static int value = first + sum_impl<IntList<other...>>::value;
    };
    ```
    secondwtq
        2
    secondwtq  
       2022-01-08 16:09:11 +08:00
    用函数貌似也不是不行: https://godbolt.org/z/jToWjMhj1
    但是很难说 concept 能帮你

    另外恭喜楼主入坑原 ... 元宇宙 ... 编程
    zeal7s
        3
    zeal7s  
    OP
       2022-01-08 17:21:07 +08:00
    @secondwtq 这个确实可行。

    另外,我想问下
    template<typename T>
    void sum() {}

    其中的 T 如果我能确保它的类型是 IntList<int...N>,那么我可以在 sum 函数中对 T 进行包展开吗?
    如果可以,那么如何把 T 变成 IntList<int... N>呢?
    secondwtq
        4
    secondwtq  
       2022-01-08 20:09:47 +08:00
    @zeal7s 我随便写的 ...
    换成
    template<typename T = void>
    constexpr int sum(IntList<>) { return 0; }
    应该也行

    你要把 T 弄成 IntList 不是不行,但是你要展开就得想办法把模板参数拿出来。这个好像最后都要 boil down 到某种形式的 pattern matching ,你只有一个 T 的话是没法做的。
    sarvatathagata
        5
    sarvatathagata  
       2022-01-08 22:14:22 +08:00
    应该这么写吧,虽然这个 concept 永远为真……
    #include <iostream>

    template<int...N>
    class IntList{};

    template<int...N>
    concept IsIntList = requires{IntList<N...>{};};

    template<int...N>
    requires IsIntList<N...>
    int sum() {
    return (N + ...);
    }

    int main() {
    std::cout << sum<1, 2>() << std::endl;
    return 0;
    }
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3852 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 00:52 PVG 08:52 LAX 16:52 JFK 19:52
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86