If i had to write a program with 10 different functions are you aloud to just make some functions templates and other functions initialized with data types like normal???

Recommended Answers

All 4 Replies

Sure, why would you not be allowed to? Are you talking about several functions with the same name (overloads). Then, yes, you are also allowed to mix function template and normal functions, even when overloading (but you should avoid specializing function templates if you provide other overloaded functions, that's the only caveat).

If that didn't answer your question, you might want to post some simple example of what you have in mind.

yes that did answer my question thank you. Also i wanted to know...when your writing an application do you have to define your objects of a class template in order to use a template function??? Can you just define an object and use it with a template function???

I don't exactly understand your questions. Once a class template is instantiated, it is no different than a normal class. Once a function template is instantiated, it is no different than a normal function. A template is just a pattern to manufacture a type or function once the template arguments are provided. Once they are provided, the generated class or function is just like a normal function (except for a few "expert-level" details you don't have to worry about at this point).

I think these examples will answer your questions:

struct foo_normal {
  int value;
};

template <typename T>
struct foo_templated {
  T value;
};

template <typename Foo>
void bar(Foo f) {
  std::cout << f.value << std::endl;
};

int main() {
  foo_normal f1; f1.value = 42;
  bar(f1);  //will print '42'
  foo_templated<int> f2; f2.value = 69;
  bar(f2);  //will print '69'
  foo_templated<double> f3; f3.value = 1.61803399;
  bar(f3);  //will print '1.61803399'
  return 0;
};

okay,yes your example helped answer my question..thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.