Templates

If possible how do you make templates carry over to other functions rather than rewriting it as such?

#include <iostream>
#include <string>
using namespace std;

template <typename T>
T add(const T arr[], unsigned els) {
T total = T(); // int() == 0, double() == 0.0, char() = '\0'
// bool() == false
for (unsigned i = 0; i < els; i++)
total = total + arr[i];
return total;
} // add

template <typename T>
void show(const T arr[], unsigned els) {
for (unsigned i = 0; i < els; i++)
cout << " " << arr[i];
cout << endl;
}

template <typename T>
void reverse(T arr[], unsigned els) {
if (els > 1)
for (unsigned i = 0, j = els - 1; i < j; i++, j--) {
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
cout << endl;
}

int main() {
double ar[]{ 1.2, 2.3, 3.4, 4.5, 5.6 };
show(ar, 5);
reverse(ar, 5);
show(ar, 5);

double arr1[]{ 1.2, 2.3, 3.4 };
string arr2[]{ "Richard", "Harry", "Joe" };
cout << "add returns " << add(arr1, 3) << endl;
cout << "add returns " << add(arr2, 3) << endl;
}
OP’s code, formatted to the best of my ability( aka not much):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
using namespace std;

template <typename T>
T add(const T arr[], unsigned els) {
  T total = T(); // int() == 0, double() == 0.0, char() = '\0'
  // bool() == false
  for (unsigned i = 0; i < els; i++)
    total = total + arr[i];
  return total;
} // add

template <typename T>
void show(const T arr[], unsigned els) {
  for (unsigned i = 0; i < els; i++)
    cout << " " << arr[i];
  cout << endl;
}

template <typename T>
void reverse(T arr[], unsigned els) {
  if (els > 1)
    for (unsigned i = 0, j = els - 1; i < j; i++, j--) {
      T temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
    }
  cout << endl;
}

int main() {
  double ar[]{ 1.2, 2.3, 3.4, 4.5, 5.6 };
  show(ar, 5);
  reverse(ar, 5);
  show(ar, 5);

  double arr1[]{ 1.2, 2.3, 3.4 };
  string arr2[]{ "Richard", "Harry", "Joe" };
  cout << "add returns " << add(arr1, 3) << endl;
  cout << "add returns " << add(arr2, 3) << endl;
}


@metametal818 please use code tags next time you post something.
To use code tags, just surround your code block with a [.code] above and a [./code] below, only take out the "." in them.
Now, looking at your code: what do you mean? Is it like this?

1
2
3
4
5
6
7
8
9
10
11
template <class T>
T inc(T a){
  a = a + 1;
  return a;
}

template <class T>
T mkNeg(T a){
  a = ~a;
  return inc<T>(a); // you want to not provide a template arg because it is the T given for mkNeg??
}


I guess maybe a really fucked up workaround might be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <typename T>
struct type_bound_funcs {
  T inc(T a){
    a = a + 1;
    return a;
  }

  T mkNeg(T a){
    a = ~a;
    return inc(a);
  }
};

int main(){
  type_bound_funcs<int>::mkNeg(7);
}


But otherwise, not really, you can’t just have the compiler assume it’s going to use the same type in a completely different function.

By the way please don’t use that example, it’s bleh. I have no other way to describe it - it’s just bleh.
Last edited on
Topic archived. No new replies allowed.