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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:38:38+00:00 2026-05-28T07:38:38+00:00

I have a data structure in my C++ program that has some attributes of

  • 0

I have a data structure in my C++ program that has some attributes of type ‘Vector’ (defined by me), and some of type ‘double’. In another place in my code, I would very much like to be able to iterate through this data structure, and perform the same arithmetic operations on each value. The operators that I will need are defined for Vector, so the code literally looks the same for what I would need to do for a Vector type and a double type.

If at all possible, I would like to avoid iterating through the ‘Vector values’ and then separately iterating through the ‘double values’.

I realize this is straining the strict-typing of C++, but is there any good way to accomplish this?

EDIT:

Right now, I’m doing some routine operations like this:

e.values.vel = k0[i].vel + k1[i].vel * c_1;
e.values.acc = k0[i].acc + k1[i].acc * c_1;
....

(e and k0[i] are the same class type)

Right now, the only thing keeping me from generalizing e’s type is the fact that some of e’s values may be vectors, and some may be doubles, and ideally this code will deal with different objects of this type which may have different numbers or kinds of values. What I want would look like:

for (int j = 0; j < e.values.size(); j ++)
{
    e.values[j] = k0[i].values[j] + k1[i].values[j]
}

Ideally, I would have e be iterable, or provide some sort of next function, but to do that, either: it would have to store it’s variables in an array which can only contain one known type, or it would provide the next function which can only return one known type.

I’m trying to find a way where I can abstract this concept so that I can iterate through something of e’s type without knowing the type of each value.

  • 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-28T07:38:39+00:00Added an answer on May 28, 2026 at 7:38 am

    You might want to consider Boost.Fusion. It allows you to turn your type into a compile-time sequence, where you can then use a compile-time-expanding foreach to call an appropriate functor for each element.

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

Sidebar

Related Questions

I have a data structure that represents C# code like this: class Namespace: string
I have a tree data structure that is L levels deep each node has
I have a speed critical multithreaded program which involves data in a tree structure.
I have a data structure defined as struct myDataStruct { int32_t header; int16_t data[8];
I have a data structure where an entity has times stored as an int
Guys, I have a data structure which has 25 distinct keys (integer) and a
I have a class (in C++), call it Data , that has thousands of
I have a winform data entry and retrieval program called CaseNotes. This has a
Say I have some data allocated somewhere in my program, like: some_type a; and
I have two data structure classes (this is a simplified version of my code)

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.