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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:52:44+00:00 2026-05-15T02:52:44+00:00

I’m trying to write a class which contains several std::vectors as data members, and

  • 0

I’m trying to write a class which contains several std::vectors as data members, and provides a subset of vector’s interface to access them:

class Mesh
{
public:
private:
  std::vector<Vector3> positions;
  std::vector<Vector3> normals;
  // Several other members along the same lines
};

The main thing you can do with a mesh is add positions, normals and other stuff to it. In order to allow an STL-like way of accessing a Mesh (add from arrays, other containers, etc.), I’m toying with the idea of adding methods like this:

public:
  template<class InIter>
  void AddNormals(InIter first, InIter last);

Problem is, from what I understand of templates, these methods will have to be defined in the header file (seems to make sense; without a concrete iterator type, the compiler doesn’t know how to generate object code for the obvious implementation of this method).

  1. Is this actually a problem? My gut reaction is not to go around sticking huge chunks of code in header files, but my C++ is a little rusty with not much STL experience outside toy examples, and I’m not sure what “acceptable” C++ coding practice is on this.

  2. Is there a better way to expose this functionality while retaining an STL-like generic
    programming flavour? One way would be something like this:

(end list)

class RestrictedVector<T>
{
public:
  RestrictedVector(std::vector<T> wrapped)
    : wrapped(wrapped) {}

  template <class InIter>
  void Add(InIter first, InIter last)
  {
    std::copy(first, last, std::back_insert_iterator(wrapped));
  }

private:
  std::vector<T> wrapped;
};

and then expose instances of these on Mesh instead, but that’s starting to reek a little of overengineering 😛 Any advice is greatly appreciated!

  • 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-15T02:52:45+00:00Added an answer on May 15, 2026 at 2:52 am

    these methods will have to be defined in the header file

    They have to be defined in a header file, so that if they’re used then they’re available in the translation unit where the template function is instantiated. If you’re worried about too many templates in header files, slowing down compilation of translation units which use Mesh but don’t actually use that template function, then you could move the implementation into a separate header file. Makes life slightly more complicated for clients, deciding whether to include the “full fat” class header or not, but it’s not actually difficult.

    Alternatively, for this particular example you could define an output iterator for Mesh, which appends Normals. Then clients with their arbitrary iterators can do:

    std::copy(first, last, mymesh.normalAdder());
    

    The only header they need with template code in it is <algorithm>, which they quite possibly have already.

    To do it yourself, the object returned by normalAdder() needs to overload operator++() and operator*(), which itself needs to return a proxy object (usually *this) which implements operator=(const &Vector3). That appends to the vector of normals. But all that is non-template code, and can be implemented in your .cpp file.

    Again in this example, normalAdder() could just return std::back_inserter(this.normals);, a template from <iterator>.

    As to whether you need to worry about it – I think when compilation times go skyward, it’s more frequently due to unnecessary dependencies rather than due to small bits of template code in headers. Some large projects seem to need drastic measures, but personally I haven’t worked with anything over about 100 files or so.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try the following. I also put together an example at… May 15, 2026 at 2:53 am
  • Editorial Team
    Editorial Team added an answer I believe the byte order is "optimized" on the iPhone… May 15, 2026 at 2:53 am
  • Editorial Team
    Editorial Team added an answer Yes! Here is a sample project illustrating the technique. Step… May 15, 2026 at 2:53 am

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.