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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:06:06+00:00 2026-05-30T12:06:06+00:00

I have a Vector class which represents a 2D vector. It is templated to

  • 0

I have a Vector class which represents a 2D vector. It is templated to allow any numerical type to be used for the x and y components. As an example, one of the arithmetic operators I overload is * for multiplying a vector with a scalar:

template <typename T, typename U>
inline const Vector<T> operator*(const Vector<T>& vector, U scalar) {
    return Vector<T>(vector.x * scalar, vector.y * scalar);
}

(I also have a function with the parameters in the opposite order to allow scalar * Vector in addition to Vector * scalar).

As you can see, I use <T, U> instead of simply <T> so that the scalar doesn’t have to be the same type as the Vector. When I didn’t do this, surprisingly Vector<double> * int wouldn’t compile (I thought the int would automatically widen).

In any case, I don’t simply want to return a Vector<T>. I want to mimic the built-in types and return whichever has higher precision, T or U. So for example, Vector<int> * double => Vector<double> while Vector<double> * short => Vector<double>.

Is this possible?

  • 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-30T12:06:07+00:00Added an answer on May 30, 2026 at 12:06 pm

    There are two solutions to this. In Pre C++11 you can write a template like:

    template <typename T, typename U>
    struct WhatWillItBe {
      typedef T result_t;
    };
    
    template <typename T>
    struct WhatWillItBe<T, double> {
      typedef double result_t;
    };
    
    // ... lots more
    

    etc. and write a lot of specialisations, then you can use that to look up the return type, e.g.:

    template <typename T, typename U>
    inline const Vector<typename WhatWillItBe<T,U>::result_t> operator*(const Vector<T>& vector, U scalar) {
        return Vector<typename WhatWillItBe<T,U>::result_t>(vector.x * scalar, vector.y * scalar);
    }
    

    Alternatively C++11 makes this straightforward, you can write auto for the return type and use -> to specify the return type after the rest of the function:

    template <typename T, typename U>
    inline auto operator*(const Vector<T>& vector, U scalar) -> Vector<decltype(vector.x*scalar)> {
        return Vector<decltype(vector.x*scalar)>(vector.x * scalar, vector.y * scalar);
    }
    

    Which allows you to use decltype for the return type of a function, setting it based on what would happen for promotion naturally with vector.x * scalar.

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

Sidebar

Related Questions

If I have a vector of objects in one class which I want to
I have a class which has a private attribute vector rectVec; class A {
I'm wondering something. I have class Polygon , which composes a vector of Line
I have a vector-like class that contains an array of objects of type T
I have a class Vector that represents a point in 3-dimensional space. This vector
I have a vector called actorVector which stores an array of objects of type
Background I have a container class which uses vector<std::string> internally. I have provided a
I have a resource_manager class which maintains a std::vector<boost::shared_ptr<resource> > internally. resource_manager is a
i have written a class which implements ListFieldCallBack like, import java.util.Vector; import net.rim.device.api.ui.Graphics; import
Say you have a vector class that has a template length and type -

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.