Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8504161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:57:03+00:00 2026-06-11T01:57:03+00:00

#include <iostream> #include <string> using namespace std; struct Uid { typedef int type; };

  • 0
#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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:57:05+00:00Added an answer on June 11, 2026 at 1:57 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <vector> using namespace std; typedef struct Record { std::string name; bool
#include <iostream> using namespace std; int main() { struct list { string name; int
#include<iostream> #include<stdlib.h> #include<string.h> #include<stdio.h> using namespace std; union type{ int a; char b; int
#include <iostream> using namespace std; typedef struct { char streetName[5]; } RECORD; int main()
#include <string> #include <iostream> using namespace std; struct foo { static const int X
#include<iostream> #include<string> using namespace std; int main(void) { struct STRCT { int num; string
#include<iostream> #include<windows.h> #include<string> #include<fstream> using namespace std; class linklist //linked list class { struct
#include <iostream> #include <string.h> using namespace std; int main() { int e=0; int b=0;
#include <iostream> #include <string> using namespace std; string crash() { } int noCrash() {
#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.