C++ pass variable type to function

You can’t pass types like that because types are not objects. They do not exist at run time. Instead, you want a template, which allows you to instantiate functions with different types at compile time:

template <typename T>
void foo() {
  cout << sizeof(T);
}

You could call this function with, for example, foo<int>(). It would instantiate a version of the function with T replaced with int. Look up function templates.

Leave a Comment

File not found.