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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:18:26+00:00 2026-05-21T10:18:26+00:00

Say I need a new type in my application, that consists of a std::vector<int>

  • 0

Say I need a new type in my application, that consists of a std::vector<int> extended by a single function. The straightforward way would be composition (due to limitations in inheritance of STL containers):

class A {
    public:
        A(std::vector<int> & vec) : vec_(vec) {}
        int hash();
    private:
        std::vector<int> vec_
}

This requires the user to first construct a vector<int> and a copy in the constructor, which is bad when we are going to handle a sizeable number of large vectors. One could, of course, write a pass-through to push_back(), but this introduces mutable state, which I would like to avoid.

So it seems to me, that we can either avoid copies or keep A immutable, is this correct?

If so, the simplest (and efficiency-wise equivalent) way would be to use a typedef and free functions at namespace scope:

namespace N {
typedef std::vector<int> A;
int a_hash(const A & a);
}

This just feels wrong somehow, since extensions in the future will “pollute” the namespace. Also, calling a_hash(...) on any vector<int> is possible, which might lead to unexpected results (assuming that we impose constraints on A the user has to follow or that would otherwise be enforced in the first example)

My two questions are:

  • how can one not sacrifice both immutability and efficiency when using the above class code?
  • when does it make sense to use free functions as opposed to encapsulation in classes/structs?

Thank you!

  • 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-21T10:18:27+00:00Added an answer on May 21, 2026 at 10:18 am

    Hashing is an algorithm not a type, and probably shouldn’t be restricted to data in any particular container type either. If you want to provide hashing, it probably makes the most sense to create a functor that computes a hash one element (int, as you’ve written things above) at a time, then use std::accumulate or std::for_each to apply that to a collection:

    namespace whatever { 
    struct hasher { 
        int current_hash;
    public:
        hasher() : current_hash(0x1234) {}
    
        // incredibly simplistic hash: just XOR the values together.
        operator()(int new_val) { current_hash ^= new_val; }
        operator int() { return current_hash; }
    };
    }
    
    int hash = std::for_each(coll.begin(), coll.end(), whatever::hasher());
    

    Note that this allows coll to be a vector, or a deque or you can use a pair of istream_iterators to hash data in a file…

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

Sidebar

Related Questions

For wxWidgets, why do you need to say: MyFrame *frame = new MyFrame instead
Let's say i need to find all .bar elements inside an element that's assigned
Let's say I need to use Python and C++. I can call Python function
Here is my scenario. For the example lets say that I need to return
Say I have a LINQ-to-XML query that generates an anonymous type like this: var
I'd like to make a new event type in JavaScript. Say I have some
Say I need some very special multiplication operator. It may be implemented in following
Say I need to call a javascript file in the <head> of an ERb
Lets say you need to attach some JavaScript functionality to an ASP.NET User Control
Lets say I need to write several functions processing some data. These functions are

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.