Static duck typing in C++

后端 未结 5 2297
日久生厌
日久生厌 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 15:14

    I'd like to omit writing a template parameter list that is repetitive and mostly meaningless (Just imagine what would happen if there are 7 ducks...)

    For that you could use variadic templates and do something like the following:

    template
    void let_them_quack(DUCK &&d) {
      d.quack();
    }
    
    template
    void let_them_quack(DUCK &&d, Args&& ...args) {
      d.quack();
      let_them_quack(std::forward(args)...);
    }
    

    Live Demo

提交回复
热议问题