mardi 12 juillet 2022

How to define a special handle function when there is a template function also match?

I have a class with template function and a special function, which looks like:

#include <bits/stdc++.h>
using namespace std;

class A { 
 public:
  template <typename T>
  void test(const T& t) {
    cout << "template " << t << endl;
  }

  void test(const std::string& s) {
    cout << "test " << s << endl;
  }
};

int main() {
  A a;
  a.test("asdas");
}

As you could see, there are two test function:

  1. template function
  2. special function which only match std::string input.

what i want is:

  1. test(1) -> call template function test<int>(1)
  2. std::string str = "asd"; test(str); -> call special function test(str)
  3. test("asd") -> call special function test(std::string str = "asd")

how to achieve this?

Aucun commentaire:

Enregistrer un commentaire