dimanche 3 février 2019

C++ why overloading (T&) in template with (T*)

in C++, if a method is accepting left reference + pointer only,

it seems it suffices if we only have a template method with T& as its parameter, why we usually overload with test(T* ) as well ?

proof of concept: left reference method can take pointer argument.

#include <iostream>
using namespace std;

template<class T>
void test(T& arg) {
  T value = arg;
  cout << *value << endl;
}

int main() {
    int b = 4;
    int* a = &b;
    test(a); // compiles and runs without issue.
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire