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

  • Home
  • SEARCH
  • 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 6250213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:22:53+00:00 2026-05-24T13:22:53+00:00

I am relatively new to c++ templates, and am implementing some numerical routines. In

  • 0

I am relatively new to c++ templates, and am implementing some numerical routines. In particular, I have been looking through some code whose style is such that if it were to define a vector, it would be as

template <class Field>
class Vector2
{
    private:
    std::vector<Field> container;

    public:
    Vector2(size_t size) : container(std::vector<Field>(size, Field(0))) {}
    Vector2(const Vector2<Field> &source) : container(source.container) {}

          Field & operator[](size_t index) { return container.at(index); }
    const Field & operator[](size_t index) const { return container.at(index); }

    Vector2<Field> operator+( const Vector2<Field> & rhs) const {
        if (rhs.container.size() != container.size())
            throw new std::invalid_argument("wrong size");

        Vector2<Field> sum(*this);
        for (int i = 0; i < container.size(); i++)
            sum[i] += rhs[i];
        return sum;
    }
};

However, if I were designing the class template, I would have chosen to specify the size in the template parameter:

template <int size, class Field>
class Vector1
{
    private:
    std::vector<Field> container;

    public:
    Vector1() : container(std::vector<Field>(size, Field(0))) {}
    Vector1(const Vector1<size, Field> &source) : container(source.container) {}

    Field & operator[](size_t index) { return container.at(index); }
    const Field & operator[](size_t index) const { return container.at(index); }

    Vector1<size, Field> operator+( const Vector1<size, Field> & rhs) const {
        Vector1<size, Field> sum;
        for (int i = 0; i < size; i++)
            sum[i] += rhs[i];
        return sum;
    }
};

My thinking is that this would prevent things like adding two vectors of different dimensions, since they would be different classes. In particular, something like

Vector1<double> a (3);
Vector1<double> b (4);
Vector1<double> c = a + b;

would need run-time checking of the sizes, yet

Vector2<3,double> a;
Vector2<4,double> b;
Vector2<5,double> c = a + b;

won’t even compile. Since this code is part of a numerical analysis package, execution speed is of the essence, and I have to assume that the authors would have considered such things. This style is used throughout the code for vectors, matrices, and other objects that are to be interpreted as elements of numerically-parametrized sets (i.e. vector spaces of varying dimension).

So, my question is,

Are there any execution-speed considerations that would imply a preference for the constructor-parameter method vs. the template-parameter method?

  • 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-24T13:22:55+00:00Added an answer on May 24, 2026 at 1:22 pm

    Of course. If the vector size is a runtime parameter, then binary operations must check at runtime that operand sizes agree.

    Ideally, a C++ numerical package should provide templates with runtime-specified sizes and compile-time-specified sizes, and they should be interchangeable. Specifying size at compile time can only improve performance.

    You could implement your suggestion by replacing std::vector in the given implementation with std::array/boost::array. If the template parameter is only used to define a runtime parameter, nothing is gained.

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

Sidebar

Related Questions

I am relatively new to C++ and I have been working on a basic
I have done some iPhone application development but still am relatively new at it.
Im relatively new to the Android world but I have a quick question. I
I'm relatively new to X Code and I m working on photo collage App
I am relatively new to Android, but I am looking to use an interactive
I'm relatively new to python/django. I'm having an issue with sending to IDs through
I'm relatively new to GAE, and I'm having some difficulty understanding the URL mappings.
I am relatively new to the whole MVC way of looking at things. I
I am relatively new to WPF and I have run into a little problem.
I've been struggling through my first Backbone app and have started to get the

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.