is it possible to make function that will accept multiple data types for given argument?

前端 未结 4 665
广开言路
广开言路 2020-12-02 16:29

Writing a function I must declare input and output data types like this:

int my_function (int argument) {}

Is it possible to make such a de

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 16:49

    Use a template:

    template 
    T my_function(T arg) {
      // Do stuff
    }
    
    int a = my_function(4);
    

    Or just overload:

    int my_function(int a) { ... }
    char my_function(char a) { ... }
    bool my_function(bool a) { ... }
    

提交回复
热议问题