Static duck typing in C++

后端 未结 5 2281
日久生厌
日久生厌 2021-02-15 14:45

C++ has some sort of duck typing for types given by template parameters. We have no idea what type DUCK1 and DUCK2 will be, but as long as they can

5条回答
  •  野性不改
    2021-02-15 14:53

    We only need to write one version of the function:

    #include 
    
    template
    void let_them_quack(Quackers&& ...quackers) {
      using expand = int[];
    
      void(expand { 0, (std::forward(quackers).quack(), 0)... });
    }
    
    struct Duck {
      void quack() {}
    };
    
    int main()
    {
      Duck a, b, c;
      let_them_quack(a, b, c, Duck());
    }
    

提交回复
热议问题