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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:38:57+00:00 2026-06-18T04:38:57+00:00

I want to write a 2d (mathematical) vector class that holds an X and

  • 0

I want to write a 2d (mathematical) vector class that holds an X and Y value. I want it to have overloads for operators such as + so I can easily use it to express mathematical formulas in my code. Consider this:

template <class T>
struct Vector
{
    T x, y;

    inline Vector<T> operator +(const Vector& other) const
    {
        return {x + other.x, y + other.y};
    }
};

As you can see I made use of C++11’s ability to use initializer lists in return statements. However, they’re still not expressions – I can’t apply operations to them. And here comes my problem. I want the class to be POD. I can’t define custom constructors so that I can initialize x and y via parameters. This is fine because I can use initializer lists for construction, e.g.:

Vector<int> foo = {1, 2};

However, I cannot use that when I need to construct another Vector inside of an expression (operator * isn’t defined but that doesn’t matter here):

Vector<int> result = (foo + {1, 2}) * 12;

As I stated before the constructor alternative is not an option. I would love to hear any input on this, because I can’t think of any solutions to this problem, other than storing {1, 2} in a named variable.

  • 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-06-18T04:38:58+00:00Added an answer on June 18, 2026 at 4:38 am

    Well the easiest option is to just create a temporary using aggregate initialisation:

    Vector<int> result = (foo + Vector<int>{1, 2}) * 12;
    

    Alternatively, you could do some magic with C++11’s user-defined literals so that something like "1,2"_v becomes one of your objects:

    Vector<int> operator "" _v(const char* literal, size_t)
    {
      std::stringstream literal_stream(literal);
      Vector<int> vec;
      literal_stream >> vec.x;
      literal_stream.ignore();
      literal_stream >> vec.y;
      return vec;
    }
    

    You could definitely do with some format checking in here though. Then you can do:

    Vector<int> result = (foo + "1,2"_v) * 12;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyway that I can write a css value as mathematical expression? Example:
I want to use partial specialization for a template class so that all children
I my project I have a class that in this class I want to
I am trying to write a Mathematica script that can interpolate a discrete vector
I want to write a class for mathematical Vectors (holding real numbers). I figured
I want to write a program in Prolog that confirms if a b-tree of
I want to write a class with three int values in them and manipulate
suppose we have two numbers i want write program which print common bits subsequent
I have a PHP-script that outputs: ((5^2x^2)^2+3)/(sqrt(sqrt(81))) I want it to be formatted properly
I want to write a Module Arg[f_,n_] that takes a function f (having <=n

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.