简体   繁体   中英

How to use member function specialization in class?

#include <iostream>
#include <string>

using namespace std;

struct Uid {
  typedef int type;  
};

struct Name {
  typedef string type;
};


struct Age {
  typedef int type;
};


template <class T1, class T2, class T3>
class People {
private:
  typename T1::type val1;
  typename T2::type val2;
  typename T3::type val3;
  //add a get function here
  }
};


int main() {
  People<Uid, Name, Age> people;
  people.get<Uid>(); //make this validate
}

this is my code, and I want to add a get function in the class to make the function call get in main validate. I try to add a tempalte get and its specialization version in the class, but it's an invalidate methond, the complier said: explicit specialization in non-namespace scope 'class People'. Someone said this method works in vs, but it break the standard.

You need a helper class that your templated get() member function can use. The helper class can be at namespace scope.

#include <iostream>
#include <string>

using std::string;
using std::cout;

struct Uid {
  typedef int type;
};

struct Name {
  typedef string type;
};

struct Age {
  typedef int type;
};

// Helper class that can be specialized to get different members of People.
template <class P, class U> struct PeopleGet;


template <class T1, class T2, class T3>
class People {
public:
  People(
    typename T1::type const& val1,
    typename T2::type const& val2,
    typename T3::type const& val3
  )
  : val1(val1),
    val2(val2),
    val3(val3)
  {
  }

  template <class U> typename U::type get()
  {
    return PeopleGet<People<T1,T2,T3>,U>::get(*this);
  }
private:
  typename T1::type val1;
  typename T2::type val2;
  typename T3::type val3;

  template <class P,class U> friend class PeopleGet;
};


template <class T1,class T2,class T3>
struct PeopleGet<People<T1,T2,T3>,T1> {
  static typename T1::type get(const People<T1,T2,T3> &people)  { return people.val1; }
};

template <class T1,class T2,class T3>
struct PeopleGet<People<T1,T2,T3>,T2> {
  static typename T2::type get(const People<T1,T2,T3> &people)  { return people.val2; }
};

template <class T1,class T2,class T3>
struct PeopleGet<People<T1,T2,T3>,T3> {
  static typename T3::type get(const People<T1,T2,T3> &people)  { return people.val3; }
};


int main()
{
  People<Uid, Name, Age> people(5,"name",47);
  cout << people.get<Uid>() << "\n";
  cout << people.get<Name>() << "\n";
  cout << people.get<Age>() << "\n";
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM