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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:13:22+00:00 2026-05-26T14:13:22+00:00

I have a template base class like this: template<typename T, std::size_t Size> class VectorT

  • 0

I have a template base class like this:

template<typename T, std::size_t Size>
class VectorT
{
public:
    typedef T data_type;
}

and a few specialised derived classes:

template<typename T>
class Vector2d : public VectorT<T, 2U>
{ // some specialised functions }

template<typename T>
class Vector3d : public VectorT<T, 3U>
{  // some other specialised functions }

and these work fine. Howerver, I have a few free-standing functions for operators. For example:

template<typename T, size_t Size>
VectorT<T, Size> operator*(T lhs, const VectorT<T, Size>& rhs)
{
    ... 
}

Unfortunately these do not work for my derived classes, because they return a VectorT<T, Size> instead of a Vector2d<T>.

So I tried with

template<V>
V operator*(typename V::data_type lhs, const V& rhs)
{
    ... 
}

and this works fine, however it can lead to ambiguities because it sucks in anything else with a data_type member.

How can I get around this: how can I write type safe functions that only work with my vector base, or any derivatives of?

I am trying to get around having to re-declare and redefine the operators again for the subclasses.

  • 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-26T14:13:23+00:00Added an answer on May 26, 2026 at 2:13 pm

    You could add yet another base class, one that is independent of the template parameters, and use SFINAE to disable calls for types other than derived from such base:

    struct VectorBase {};
    
    template< typename T, std::size_t Size >
    class VectorT : public VectorBase { ... }
    
    template< typename V >
    typename boost::enable_if< boost::is_base_of< VectorBase, V >, V >::type
    operator*( V lhs, V const& rhs ){ ... }
    

    Note that is_base_of< X, X > is always true, so this function will work for one more type than required, namely the base class VectorBase.

    If you are using a compiler that implements TR1, you can replace boost:: for std:: in both places where is used.

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

Sidebar

Related Questions

I have something like this: class Base { public: static int Lolz() { return
I have a base class that is a template that looks like this: template
I have template that looks like this: 100 template<size_t A0, size_t A1, size_t A2,
I have a subset of a pointer class that look like: template <typename T>
I cannot figure this out. I need to have an abstract template base class,
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
I have implemented a templated buffer class like this: template <class T, class Impl>
I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
I am new to STL. I have written a Template Base class as follows
I have the following code that doesn't compile. class Base { public: virtual ~Base()

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.