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 7128869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:11:00+00:00 2026-05-28T11:11:00+00:00

I would like to templatize first type of std::pair using the following construction template

  • 0

I would like to templatize “first” type of std::pair using the following construction

template <typename T>
struct TPair
{
typedef std::pair <T, short> Type;
};

and create a vector of such pairs.

template <typename T>
struct TPairs
{
typedef std::vector <TPair <T> > Type;
};

But this code seems to be screwed for common usage and it is uncomfortable:

TPair <double> ::Type my_pair (1.0, 0 ); //Create pairs
TPair <double> my_pair2 (1.0, 0 ); //Create object, needs a constructor

TPairs <double> ::Type pairs; //Create vector
TPairs <double> pairs2; //Create object

pairs.push_back(my_pair); //Need a constructor
pairs2.push_back(my_pair); //No push_back method for the structure...
....

Is there any more simple and comfortable solution?

  • 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-05-28T11:11:00+00:00Added an answer on May 28, 2026 at 11:11 am
    template <typename T>
    struct TPairs
    {
      typedef std::vector <TPair <T> > Type;
    };
    

    There’s a problem here: you’re creating a type that is a vector of TPair<T>, which is in fact not what you want. You want a vector of TPair<T>::Type.

    template <typename T>
    struct TPairs
    {
      typedef std::vector <typename TPair <T>::Type > Type;
    };
    

    As for your use cases, remember that those two structs you created are there just to simulate a template typedef, you should never instantiate them at all, just use their Type member typedef. So:

    TPair <double> ::Type my_pair (1.0, 0 ); // Good, creates a std::pair
    TPair <double> my_pair2 (1.0, 0 ); // Not good, does not create an std::pair
    
    
    TPairs <double> ::Type pairs; //Good, creates a vector
    TPairs <double> pairs2;       //Not good, doesn't create a vector
    
    pairs.push_back(my_pair);   // Ok, does what you mean
    pairs2.push_back(my_pair);  // Can't compile, the `TPairs` struct ins't a vector
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C++0x I would like to write a function like this: template <typename... Types>
I have a file template.txt which contains the following: Hello ${something} I would like
I would like some way to get the first parameter type of a lambda
I am hosting SpiderMonkey in a current project and would like to have template
I would like to create a template for new stored procedures for our development
I would like to have a nice template for doing this in development. How
I would like to learn how I can use nested template tags where the
I would like to create the ASP.NET User database template on a database of
Instead of hardcoding the default @author template I would like Eclipse to use user's
I'm building a HTML template for my site and would like to have a

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.