2

I have a simple generice arithmetic vector class and want to implement the * operator for scalar multiplication:

template<class Value_T, unsigned int N>
class VectorT
{
public:
    typedef Value_T value_type;
    typedef unsigned int size_type;

    size_type GetNumElements() const { return N; }      

    // class member
    // elements in the vector
    value_type elements[N];     

    // the number of elements
    static const size_type size = N;
};

// scalar multiplication
template<class Value_T, unsigned int N>
const VectorT<Value_T, N> operator*(const VectorT<Value_T, N>& v, Value_T s)        
{
    VectorT<Value_T,N> vTemp(v);
    for (unsigned int i = 0; i < v.GetNumElements(); i++)
    {
        vTemp.elements[i] *= s;
    }
    return vTemp;
}

Using it like this ...

typedef VectorT<double, 3> Vec3;

int main(int argc, char* argv[])
{
    Vec3 v;
    Vec3 v2 = v * 2; // multiply with scalar int
    return 0;
}

... gives compiler error C2782 (MSVC2012) that the template parameter Value_T for the * operator is ambiguous: int or double.

If I define the *operator within my class as friend function the error is gone and it works fine for scalar int or double. But actually there is no need here for friend declaration as the public interface of class VectorT is sufficient for the operator (In my real code I have the members privat and some public accessor methods).

I want the scalar multiplication work only for the Value_T type: For a VectorT<int, N> I want only integer values be given to the *operator. I further want the *operator being commutativ. That's why I don't implement it as a simple member function, where the lefthand operator is always of type VectorT.

How to implement * operator as non-member and non-friend here?

1
  • 1
    Is there a reason why the scalar must be of the exact same type as the value type of your VectorT? If not, add a(n independent) template type parameter for the second argument. You could also disable type deduction for the second argument by using the function parameter type identity<Value_T>::type, where identity is the identity type function.
    – dyp
    Oct 12, 2014 at 18:34

3 Answers 3

4

Look at the operator's definition

const VectorT<Value_T, N> operator*(const VectorT<Value_T, N>& v, Value_T s) 

and on the line

Vec3 v2 = v * 2; // multiply with scalar int

operator* is called with parameters of type VectorT and int. So Value_T is double once and int the other time. You could multiply with 2.0 to resolve the ambiguity or add a third template parameter to the operator* definition:

template<class Value_T, unsigned int N, class ScalarType>
const VectorT<Value_T, N> operator*(const VectorT<Value_T, N>& v, ScalarType s)
3

The answers posted so far suggest giving an independent template parameter to the type of function parameter s. This is a viable approach, but not the only one.

Alternatively, a better solution might be to exclude your second argument from the template argument deduction process by intentionally placing it into non-deduced context

// scalar multiplication
template<class Value_T, unsigned int N>
const VectorT<Value_T, N> operator*(const VectorT<Value_T, N>& v, 
                                    typename VectorT<Value_T, N>::value_type s)        
{
  ...
}

That way the compiler will deduce the template arguments from the type of v parameter, but not from the type of s parameter. And s will still have the proper type, just like you wanted it to.

The above approach takes advantage of the fact that your class template already provides a convenient inner typename value_type. In a more general case one can achieve the same by using an identity template

template <typename T> struct identity
{
  typedef T type;
};

template<class Value_T, unsigned int N>
const VectorT<Value_T, N> operator*(const VectorT<Value_T, N>& v, 
                                    typename identity<Value_T>::type s)        
{
  ...
}

The C++ standard library decided not to include std::identity template, since its functionality is apparently covered by std::decay and std::remove_reference. So, by using standard templates you can implement the general solution as

template<class Value_T, unsigned int N>
const VectorT<Value_T, N> operator*(const VectorT<Value_T, N>& v, 
                                    typename std::decay<Value_T>::type s)        
{
  ...
}

The following article mentions this specific matter http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3766.html

4
  • That is exactly what I wanted. For VectorT<int, N> I only want integer values to be given as scalar, and the scalar multiplication shall only be done with integer. An additional independent template parameter solves the compiling problem but if I put a double into scalar multiplication of an integer vector, the multiplication is done with double and only the result is truncated to int. I want it to work like an mathematical field in abstract algebra. Oct 12, 2014 at 19:44
  • Do you have an idea why the compilation works when I implement this operators as friend method inside the class? Type deduction seems not be a problem then anymore. Oct 12, 2014 at 19:48
  • 1
    @Michbeckable: I guess that when you implemented it inside the class, you didn't declare your operator as an independent template, did you? In that case, there's no argument type deduction at all. Once you manually specified the arguments for the class template VecorT<double, 3>, you essentially generated that friend operator (declared inside the template class) with already known, fixed parameter types. Oct 12, 2014 at 19:54
  • Yes, right, I did not specify it as independent template, just defined it within the template class, so no template type deduction was involved. Oct 12, 2014 at 20:59
2

First of, you could use std::array instead of implementing VectorT. Nevertheless, the problem you have is due to the fact that the integral literal 2 in the expression Vec3 v2 = v * 2; is of type int. Consequently, the compiler can't deduce to the overloaded operator* of yours, since as it is implemented it works only in the cases where your VectorT contains elements of the same type with the multiplier.

In order to overcome this, you could add an additional template argument to your overloaded operator* like the example below:

template<class Value_T1, class Value_T2, unsigned int N>
VectorT<Value_T1, N> operator*(const VectorT<Value_T1, N>& v, Value_T2 s)        
{
    VectorT<Value_T1, N> vTemp(v);
    for(unsigned int i(0), sz(v.GetNumElements()); i < sz; ++i) {
        vTemp.elements[i] *= s;
    }
    return vTemp;
}

LIVE DEMO

1
  • "since as it is implemented it works only in the cases" "It" refers to deduction or operator*? The problem, as Matthias correctly describes, is that deduction of Value_T fails since there are two contradictory results of the deduction. If you "disable" deduction by explicitly passing the template argument, the operator* function can be called.
    – dyp
    Oct 12, 2014 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.