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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:35:02+00:00 2026-05-30T17:35:02+00:00

I am implementing some classes to represent mappings (in the mathematical sense), i.e., f

  • 0

I am implementing some classes to represent mappings (in the mathematical sense), i.e., f : R x R^n -> R^n. I would like to implement an abstract base class that either:
1) Takes a std::array as a reference and modifies it
or
2) returns a std::array (by value or by reference, not sure?)

The syntax of option 2 is more desirable to me simply because I think the code will look more like the math I’m trying to represent, but I want to make sure I am not needlessly copying things and inducing a bunch of undesired overhead. For example, if I had:

// Function f declaration
std::array<double, 4> f(double t, const std::array<double, 4> & x);

// Some code snippet that uses f
std::array<double, 4> x = {0.0, 1.0, 2.0, 3.0};
double t = 0.0;
std::array<double, 4> dxdt = f(t, x);

How can I determine whether a copy is being performed on the last line, or how can I ensure that it doesn’t happen?

Within the definition of f(), what would I need to do (if anything) to ensure that this is returned without a call to the copy constructor? I want to keep the usage simple so clients don’t need to use pointers or smart pointers, but maybe this is necessary?

I know I can change this to return void and just make one of the arguments be std::array dxdt, but I like the return value syntax better, as long as there is no performance penalties or memory leak issues.

  • 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-30T17:35:03+00:00Added an answer on May 30, 2026 at 5:35 pm

    It should be something you shouldn’t care, since optimizing copies is a compiler problem. But compilers are still Turing-based machines that cannot invent optimization themselves.

    As far I know most of today’s compilers behave, if a function:

    • have a single return statement or …
    • have multiple returns returning all a same variable

    The compiler allocates that variable on the stack in the place it has to be to be evaluated by the expression the function call belongs.
    No copy are generated for that.

    If your function have multiple exits returning different expressions this -apart some particular cases- cannot be done.

    So, in general, if you have to return some wide object, just declare it and make sure all the return statements return just it.

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

Sidebar

Related Questions

I have some classes that look like this: MODEL public abstract class BaseEntity<O extends
I'm implementing some abstract classes that extend Actor and provide some extra functionality [1].
I have a base class: abstract class Base and some derived classes: class Derived:
I'm implementing some code generators, i would like to know if there's any way
i have a little problem implementing some serialization/deserialization logic. I have several classes that
I'm exporting some classes implementing the IFoo interface like this public interface IFoo {
What's the purpose of implementing the IDisposable interface? I've seen some classes implementing it
I am implementing some networking stuff in our project. It has been decided that
I am going to start implementing some unit tests for a codebase that is
in a bash script I am implementing some functions, that take parameters The problem

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.