I'm currently working on implementing some mathematical base operations and try to avoid using third party libraries as much as possible. I'm stuck at overloading the operator* for the multiplication of a Scalar*Vector and Vector*Scalar. The current code for the dot product of Scalar*Vector:
#include <vector>
#include <type_traits>
template<class Vector, class Scalar>
typename std::enable_if<std::is_floating_point<Scalar>::value, Vector>::type operator*
(
const Scalar &a,
const Vector &b
)
{
return Vector
(
a*b[0],
a*b[1],
a*b[2]
);
}
int main()
{
const std::vector<double> v1({1,2,3});
const double s1(2);
const auto result(s1*v1);
std::cout<< result << std::endl;
}
The compiler error message is:
error: invalid operands to binary expression ('double' and 'const std::vector')
Any advises on how to overload the * operator, so that both dot-products are possible? I do not intend to implement these two operators in a custom vector class, as overloaded operators. Rather than that, I aim for the templated operator.
I've found a great explanation here and used it to adjust it for the vector operations. In the vector class header file, you basically define via the struct is_vector that anything of type T is not a vector by default. In the following a all types that can act as a vector have to be listed explicitely, such as std::vector.
#include <vector>
#include <type_traits>
template <typename T>
struct is_vector
{
static const bool value = false;
};
template <>
struct is_vector< std::vector >
{
static const bool value = true;
};
template <class Vector>
typename std::enable_if<std::is_vector<Vector>::value, double>::type
operator*(const Vector &a, const Vector &b);
And in the executable, the code looks the same.
int main()
{
const std::vector<double> v1({1,2,3});
const double s1(2);
const auto result(s1*v1);
std::cout<< result << std::endl;
}
Related
I'm implementing a cache to save function calls.
Let's say I have 2 doubleparameters to my function call.
Those must be the key of some LRU cache or - to make it simpler - a C++ std::map.
So I created a template class with the array inside (variable number of values)
template <int n>
class DoubleArray
{
public:
double array[n];
};
When trying to use that as a key of my std::map, the compiler complained because it needed a operator< for those.
.....\include\c++\7.3.1\bits\stl_function.h:386:20: note:
'const DoubleArray<2>' is not derived from 'const std::map<_Key, _Tp, _Compare,
_Alloc>'
{ return __x < __y; }
~~~~^~~~~
So I implemented a comparison operator (well, I though that hashing could do the trick but it doesn't seem so...) and it compiled:
#include <map>
template <int n>
class DoubleArray
{
public:
double array[n];
bool operator<(const DoubleArray &other) const
{
return (array[0] < other.array[0]) || (array[0] == other.array[0] && array[1] < other.array[1]);
}
};
int main()
{
std::map<DoubleArray<2>,double> my_cache;
DoubleArray<2> params;
// clumsy way to initialize the array...
params.array[0] = 12;
params.array[1] = 2;
// put a value in cache
my_cache[params] = 23;
}
Note that the comparison operator is really clumsy. What if I have 6 parameters (which is my real case).
How to create a generic comparison operator (maybe using template recursion) ?
In case this is a XY problem, is there a simpler way to create a n-value key map with double types?
(note that I'm fully aware that using double values as keys looks bad, but my goal is to cache values on function calls where the parameters are exactly the same, those are not intended to be stored or such)
You are looking for std::lexicographical_compare
bool operator<(const DoubleArray &other) const
{
return std::lexicographical_compare(array, array + n, other.array, other.array + n);
}
Alternatively, you can just define an alias to std::array, which already has all the comparison operators defined
template<int n>
using DoubleArray = std::array<double, n>;
You can sidestep the issue by using std::array. Using an alias declaration your code could be simplified to
template <std::size_t N>
using DoubleArray = std::array<double, N>;
int main()
{
std::map<DoubleArray<2>,double> my_cache;
my_cache[{12, 2}] = 23;
}
Don't reinvent the wheel, use std::array. It already has an overloaded operator<. Always consider using and combining what the standard library and other well known libraries offers before writing your own custom solution: Use libraries whenever possible.
You may then declare your map like this:
std::map<std::array<double, 2>, double> my_cache;
How to create a generic comparison operator (maybe using template recursion) ?
You can try this method:
#include <utility> // std::index_sequence
#include <tuple> // std::tie
template <int N>
struct DoubleArray
{
private:
template <size_t ... Is>
bool opHelper(const DoubleArray& rhs, std::index_sequence<Is...>) const
{
return std::tie(arr[Is]...) < std::tie(rhs.arr[Is]...);
}
public:
double arr[N];
bool operator<(const DoubleArray& rhs) const
{
return opHelper(rhs, std::make_index_sequence<N>{});
}
};
If I have a generic struct/class:
template<typename T>
struct Container
{
T value;
Container(const Value& value) : value(value) { }
};
And I want to perform an operation on two of them:
template<typename T, typename U>
Container<T> operator+(const Container<T>& lhs, const Container<U>& rhs)
{
return Container<T>(lhs.value + rhs.value);
}
The problem is that if lhs is of the type Container<int> and rhs is of the type Container<float>, then I'll get a Container<int> back. But if I were to do auto result = 2 + 2.0f, then result would of of type float. So the behavior is inconsistent between builtin types and custom types.
So how would I take the operator+ overload and make it return Container<float>, much like how C++ handles arithmetic promotion with builtin types?
As far as you use one of the two types of the template, you risk to induce a cast on the result of the sum. As an example, if you accidentally choose int as your target type, even though the sum results in a float, it will be cast down to the chosen type.
Anyway, starting with C++11, you con rely on the decltype specifier as in the example above (or at least, you can do that if Container::T and Container::U are a types for which the + operator is defined).
I used also the auto specifier as return value for the operator+, for it is at our disposal starting from C++14 and it's really useful indeed.
It follows the working example above mentioned:
#include <iostream>
#include <vector>
template<typename T>
struct Container
{
T value;
Container(const T& value) : value(value) { }
};
template<typename T, typename U>
auto operator+(const Container<T>& lhs, const Container<U>& rhs)
{
return Container<decltype(lhs.value+rhs.value)>{lhs.value + rhs.value};
}
int main()
{
Container<int> c1{1};
Container<float> c2{0.5};
std::cout << (c1+c2).value << std::endl;
}
i need to dynamically create arrays containing different numerical types, including char, int, unsigned int, float, double. i'd like to be able to create any two of these arrays and, assuming they're of the same length, implement arithmetic operators such as operator+=
i've been able to implement the array as a template, but i don't know how to implement any arithmetic operators, since i can't know what the other array's type will be at compile time, or even when the first array is created (i will know the type for the array i'm constructing). i looked at std::array, but it doesn't support arithmetic operators. another alternative, which is definitely not elegant (but does work), is to implement a series of type specific operators, such as
MyArray<V> operator+ (const MyArray<float>& addend) const;
MyArray<V> operator+ (const MyArray<double>& addend) const;
MyArray<V> operator+ (const MyArray<int32>& addend) const;
MyArray<V> operator+ (const MyArray<int16>& addend) const;
thanks for any advice.
Alright, its probably obvious enough from my comments of everything in this thread that this is a particular sore spot for me. And this is for good reason, I was once like you. I was like, I can overload operators! AMAZING! OVERLOAD ALL THE OPERATORS (this was with a custom image container type). After a while, a few things became clear:
Operators are hard to correctly declare, especially templated ones.
Templated operators cannot have their types set explicitely, only implicitly.
Operation order doesn't make sense all the time.
Operators must either use exceptions as their "fail" mode which is not ideal in all cases, or use "enable-if" type syntax if the fail can be detected at compile time.
Operator MEANING is hard to document/elucidate. Different interpretations of what an operator should "do" makes it hard to figure out. (Should the MyArray<T>+MyArray<J> work as a memberwise plus like T+J or should it work like a concatenate like 'string+string'?)
Operators must return by value, which can cause overhead if your moves aren't all set up correctly/you aren't in C++11/for any reason return value elision doesn't happen.
Overall, writing your own container types is a great way to redo alot of work the STL has already done.
You COULD do it like (at a namespace scope) (assuming you have a templated cast operator available)
template <typename T, typename J>
MyArray<decltype(T()+J())> operator+(const MyArray<T>& x,const MyArray<J>& y)
{
using K=decltype(T()+J());
MyArray<K> ret(x.size());//or something?
for (size_t i = 0; i < x.size(); i++) {ret[i]=x[i]+y[i];}//could replace with foreach
return ret;
};
Though using the following with vectors just makes more sense. You can wrap it in a "add" call if you want.
std::vector<T> a;//or whatever
std::vector<J> b;//or whatever
std::vector<K> c(a.size());//note: you can still use the decl type here, OR just define it to whatever you actually want it to be
std::transform(a.begin(), a.end(). b.begin(), c.begin(), std::plus<K>());
If you are trying to do this all over the place, and are trying to make a matrix math library, use one like Eigen, it'll save you a lot of work, it'll be strongly typed as a matrix and not a generic collection, and it'll be done with the full math knowledge the Eigen team has.
You can use one more template parameter:
template<class V, class T> MyArray<V> operator+ (const MyArray<T>& addend) const;
Then the cast will always be according to your main array type.
You will likely have to dispatch your operations by a result type selected by some type traits.
Simplified for a number (no vector):
#include <iostream>
template <typename T>
struct Number {
T value;
Number(T value) : value(value) {}
template <typename U>
explicit Number(U value) : value(value) {}
operator T () const { return value; }
};
#define C_PLUS_PLUS_11 (201103L <= __cplusplus)
template <typename U, typename V>
struct binary_operation_traits {
#if C_PLUS_PLUS_11
typedef decltype(U() + V()) result_type;
#endif
};
#if ! C_PLUS_PLUS_11
template <typename T>
struct binary_operation_traits<T, T> {
typedef T result_type;
};
template <>
struct binary_operation_traits<int, float> {
typedef float result_type;
};
template <>
struct binary_operation_traits<int, double> {
typedef double result_type;
};
// And so on ...
#endif
template <typename U, typename V>
Number<typename binary_operation_traits<U, V>::result_type>
operator + (const Number<U>& a, const Number<V>& b) {
typedef typename binary_operation_traits<U, V>::result_type result_type;
return Number<result_type>(result_type(a) + result_type(b));
}
int main ()
{
Number<int> a(1);
Number<double> b(1.5);
std::cout << a + b << '\n';
return 0;
}
I have a piece of code to use the RTTI as case selector
#include <iostream>
#include <vector>
#include <typeinfo>
#include <complex>
#include <algorithm>
using namespace std;
typedef complex<int> data;
typedef std::vector< data > List;
template <typename T> void zeros(T &v)
{
if (typeid(typename T::value_type)==typeid(complex<int>))
std::fill(v.begin(), v.end(), complex<int>(0, 0));
else std::fill(v.begin(), v.end(), static_cast<typename T::value_type>(0));
}
int main(int argc, char *argv[])
{
List A;
zeros(A);
return 0;
}
In above code, I use typename T::value_type to determine the data type of the list, if there is complex then I will fill the list with complex zero. Otherwise, make it zero of the corresponding type. In above code, if I set data to be int, it does compile and run without any problem. But if I set it another other type, it fail to compile. I wonder if there is any way to make above code work for different type of data (for float, double, long, int, complex, complex and complex).
p.s. the reason I want to fill the vector in two cases because I am going to create my own complex for some reason and I have to tell the code how to create the complex zero or real zero. But for testing, I can't even make it work with built-in datatype
It seems that you want both a default initialized complex (0, 0), and a zero initialized int. You can achieve this using value initialization:
template <typename T> void zeros(T &v)
{
std::fill(v.begin(), v.end(), typename T::value_type{});
}
If you really want to perform some branching depending on what is T::value_type, then it would be better to use some type trait, like the following one:
template<typename T>
struct default_value {
static const T value;
};
template<>
struct default_value<std::complex<int>> { // assuming you'll use other complex type
static const std::complex<int> value;
};
template<typename T>
const T default_value<T>::value = T{};
const std::complex<int> default_value<std::complex<int>>::value = std::complex<int>(0, 0);
// now use it
template <typename T> void zeros(T &v)
{
std::fill(v.begin(), v.end(), default_value<typename T::value_type>::value);
}
But again, default construction and value initialization for built in types should be enough.
I am writing a template class function comparing std::strings. std::string is the template parameter. My problem is that I can't compare two const string with "==" operator, then I think I create two non-const temporary string variables to performe the comparation, but it still can't compile. Don't know why.
class VGraph is instanced as VGraph<std::string, std::string> myGraph;
template <typename V, typename E>
size_t VGraph<V, E>::find(const V& vert)
{
V temp = vert; // (1)
for (size_t i=0; i<graph.size(); i++)
{
V noneConst = graph[i].getVertex(); // (2)
if (temp==noneConst)// I think broblem is here, and tried to fix using (1)(2)
return i;
}
return graph.size();
}
Prototype of related function
template <typename V, typename E>
const V& VVertex<V, E>::getVertex();
You probably forgot an explicit:
#include <string>
The std::string class is defined by a another header you included, but not the operator ==.