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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:18:42+00:00 2026-05-22T14:18:42+00:00

Instead of writing town->first I would like to write town->name . Inline named accessors

  • 0

Instead of writing town->first I would like to write town->name. Inline named accessors (Renaming first and second of a map iterator and Named std::pair members) are the best solutions I have found so far. My problem with named accessors is the loss of type safety:
pair<int,double> may refer to struct { int index; double value; } or to struct { int population; double avg_temp; }. Can anyone propose a simple approach, perhaps something similar to traits?

I often want to return a pair or a tuple from a function and it is quite tiring to introduce a new type like struct city { string name; int zipcode; } and its ctor every time. I am thrilled to learn about boost and C++0x but I need a pure C++03 solution without boost.

Update

Re andrewdski’s question: yes, a (hypothetical) syntax like pair<int=index, double=value> which would create a distinct type from pair<int=population, double=avg_temp> would meet your requirement. I do not even mind having to implement a custom pair/tuple template class ONCE and just passing a ‘name traits’ template argument to it approprietly when I need a new type. I have no idea how that ‘name traits’ would look like. Maybe it’s impossible.

  • 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-22T14:18:42+00:00Added an answer on May 22, 2026 at 2:18 pm

    I don’t see how you can possibly do better than

    struct city { string name; int zipcode; };
    

    There’s nothing non-essential there. You need the types of the two members, your whole question is predicated around giving names to the two members, and you want it to be a unique type.

    You do know about aggregate initialization syntax, right? You don’t need a constructor or destructor, the compiler-provided ones are just fine.

    Example: http://ideone.com/IPCuw


    Type safety requires that you introduce new types, otherwise pair<string, int> is ambiguous between (name, zipcode) and (population, temp).

    In C++03, returning a new tuple requires either:

    city retval = { "name", zipcode };
    return retval;
    

    or writing a convenience constructor:

    city::city( std::string newName, int newZip ) : name(newName), zipcode(newZip) {}
    

    to get

    return city("name", zipcode);
    

    With C++0x, however, you will be allowed to write

    return { "name", zipcode };
    

    and no user-defined constructor is necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, instead of writing plain old Html to build a form, I would like
I would like to chain my own methods in Ruby. Instead of writing ruby
Instead of writing ((x: Double) => (((y: Double) => y*y))(x+x))(3) I would like to
Instead of writing: $('div').css({'backgroundColor': 'red'}); I want to write something like: $('div').css({get_property_name(): 'red'}); where
I'm just getting started on writing functions instead of writing everything inline. Is this
I’m confused about this; instead of writing tweepy codes like- auth=tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret)
In NANT,if I write a path, how could I , instead of writing c:\documents
Instead of writing int i = new int(); i = 7; One can write
I have a control that I would like to be static, but instead of
Does C# has feature like Java's static imports? so instead of writing code like

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.