C++ function dynamic data type definition

in C++, when you define a function which takes one argument, you have to define the data type of that variable:

void makeProccess(int request)

However, I want to implement a function which takes different data types rather taking statically defined integer type.

void makeProccess(anyType request)

How can I design a proccess like this, any idea?



Use templates:

template <typename T> void makeProcess(T request) { // request is of type "T", which can vary cout << "request: " << request; }

An additional benefit, is you can specialize it:

template <> void makeProcess(string request) { cout << "This is special handling for a string request: " << request; }


You'll want to look up C++ Templates - here's a good link: http://www.cplusplus.com/doc/tutorial/templates/


Have You consider using tamplates for that ?

template <class T> void makeProcess(T request) { // code }


Firstly, the "use templates" answers are very useful - you should investigate templates - this is another alternative to those.

If the function is passing the value through to some other code that ultimately knows exactly what type is "inside", you can also use boost::any - see http://www.boost.org/doc/libs/1_42_0/doc/html/any.html. This can be a bit dangerous, though, because you can easily get code that is coupled/interdependent in ways that aren't obvious, and that crashes at runtime instead of failing to compile (which is what would happen with templates). However, it can be significantly more understandable to non-expert C++ coders than large quantities of template code.

(Note that boost::any also requires that the type is copyable and assignable.)


  • set ruby hash element value by array of keys
  • Hide UIView starting from Bottom on Scroll
  • Getting rollback in creating shortcuts on installation of node js in windows 7
  • Storing Data in a Variable vs Inline Arithmetic
  • Parsing String according to Dates in Python
  • LDA: Why sampling for inference of a new document?
  • “class template has already been declared as a non-class template”
  • How to shorten this expression using regex
  • javascript XSLT nodes, selecting the first of a group (merge-like)
  • closing WCF proxy
  • Member function pointer cast, from Derived to Base class
  • How to record a JNLP/ Java Web Start application with JMeter
  • Is an if-let or a normal if condition better?
  • MySql - get days remaining
  • What's the essential difference between these two variadic functions?
  • std::system Exception when instantiating a singleton object
  • How to make nicEditor snaplet? (Several questions)
  • Partial specialization of a class template in derived class affects base class
  • Converting simple MySQL database to a NoSQL solution
  • Neo4j: Legacy Indexes and auto index vs new label bases schema indexes
  • ckeditor and jquery UI dialog not working
  • c++ regex_replace not doing intended substitution
  • Most efficient way to move table rows from one table to another
  • VBA Excel, loop through variables
  • Disable Kendo Autocomplete
  • Django invalid literal for int() with base 10
  • How does document.ready work with angular element directives?
  • Reduction and collapse clauses in OMP have some confusing points
  • xtable package: Skipping some rows in the output
  • C++ Partial template specialization - design simplification
  • Lost migrations and Azure database is now out of sync
  • Unanticipated behavior
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal