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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:36:11+00:00 2026-05-13T14:36:11+00:00

Is there, perhaps in boost, consistent element access semantics which works across containers? something

  • 0

Is there, perhaps in boost, consistent element access semantics which works across containers?
something along the lines of:

element_of(std_pair).get<1>();
element_of(boost_tuple).get<0>();
element_of(pod_array).get<2>();

in principle i can write myself, but I would rather not reinvent the wheel.thanks

  • 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-13T14:36:11+00:00Added an answer on May 13, 2026 at 2:36 pm

    I’m not aware of such a thing.

    You could most probably just implement a free get function for the types you’re interested in. Boost.Tuple already has it. std::pair has it in C++0x. And the rest shouldn’t be too complicated.

    E.g

    #include <iostream>
    #include <utility>
    #include <vector>
    #include <boost/tuple/tuple.hpp>
    
    namespace getter
    {
        template <size_t Index, class Container>
        typename Container::reference get(Container& c)
        {
            return c[Index];
        }
    
        template <size_t Index, class Container>
        typename Container::const_reference get(const Container& c)
        {
            return c[Index];
        }
    
        template <size_t Index, class T>
        T& get(T *arr)
        {
            return arr[Index];
        }
    
        namespace detail {
            template <size_t Index, class T, class U>
            struct PairTypeByIndex;
    
            template <class T, class U>
            struct PairTypeByIndex<0u, T, U>
            {
                typedef T type;
                type& operator()(std::pair<T, U>& p) const { return p.first; }
                const type& operator()(const std::pair<T, U>& p) const { return p.first; }
            };
    
            template <class T, class U>
            struct PairTypeByIndex<1u, T, U>
            {
                typedef U type;
                type& operator()(std::pair<T, U>& p) const { return p.second; }
                const type& operator()(const std::pair<T, U>& p) const { return p.second; }
            };
        }
    
        template <size_t Index, class T, class U>
        typename detail::PairTypeByIndex<Index, T, U>::type& get(std::pair<T, U>& p)
        {
            return detail::PairTypeByIndex<Index, T, U>()(p);
        }
    
        template <size_t Index, class T, class U>
        const typename detail::PairTypeByIndex<Index, T, U>::type& get(const std::pair<T, U>& p)
        {
            return detail::PairTypeByIndex<Index, T, U>()(p);
        }
    
        using boost::get;
    }
    
    int main()
    {
        boost::tuple<int, int> tuple(2, 3);
        std::cout << getter::get<0>(tuple) << '\n';
        std::vector<int> vec(10, 1); vec[2] = 100;
        std::cout << getter::get<2>(vec) << '\n';
        const int arr[] = {1, 2, 3, 4, 5};
        std::cout << getter::get<4>(arr) << '\n';
        std::pair<int, float> pair(41, 3.14);
        ++getter::get<0>(pair);
        const std::pair<int, float> pair_ref = pair;
        std::cout << getter::get<0>(pair_ref) << ' ' << getter::get<1>(pair_ref) << '\n';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 306k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can probably always set another association in the registry… May 13, 2026 at 9:21 pm
  • Editorial Team
    Editorial Team added an answer You can assert that the two Sets are equal to… May 13, 2026 at 9:20 pm
  • Editorial Team
    Editorial Team added an answer Anything in your application that isn't tested is most likely… May 13, 2026 at 9:20 pm

Related Questions

We have large (100,000+ elements) ordered vectors of structs (operator < overloaded to provide
Allegedly you cannot just erase/remove an element in a container while iterating as iterator
I'm interested in actual examples of using fixed point combinators (such as the y-combinator
I need to execute a program and retrieve its stdout output in c++. I'd
I'm trying to create a sparse vector class in C++, like so: template<typename V,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.