Add a macro to control if the result of binary operators should be casted.

It allows to suppress warnings when combining vectors of a type that
gets promoted to int after the binary operator is applied, for example.
This commit is contained in:
King_DuckZ 2016-11-02 01:14:23 +01:00
parent 6ab4d586eb
commit f51c739a48
2 changed files with 21 additions and 0 deletions

View File

@ -29,6 +29,7 @@ You still need to type some code in order to get started using the vector wrappe
## Features ##
### Build time configuration ###
* VWR_WITH_IMPLICIT_CONVERSIONS (default: not defined) Enable implicit conversions between wrappers of vectors of different type (see Automated conversions).
* VWR_STATIC_CAST_RESULTS (default: not defined) static_cast the result of binary operators in order to silence some compiler warnings. For example, when compiling with gcc -Wconvertion, given three short int variables a, b and c, c=a+b will generate a warning as a+b gets promoted into an int. Enabling this macro causes the previous example to become c=static_cast<short int>(a+b).
### Automated conversion ###
You can assign or construct a `Vec<A>` from a `Vec<B>`, provided they have the same dimensions and the assignemnt operator is able to convert B's scalar type to that of A. You need to define VWR_WITH_IMPLICIT_CONVERSIONS for this to work.

View File

@ -360,37 +360,57 @@ namespace vwr {
template <typename V1, typename V2>
inline Vec<typename std::common_type<V1, V2>::type> operator+ (const Vec<V1>& parLeft, const Vec<V2>& parRight) {
typedef typename VectorWrapperInfo<typename std::common_type<V1, V2>::type>::scalar_type scalar_type;
static_assert(static_cast<int>(VectorWrapperInfo<V1>::dimensions) == static_cast<int>(VectorWrapperInfo<V2>::dimensions), "Dimensions mismatch");
Vec<typename std::common_type<V1, V2>::type> retval;
for (int z = 0; z < VectorWrapperInfo<V1>::dimensions; ++z) {
#if defined(VWR_STATIC_CAST_RESULTS)
retval[z] = static_cast<scalar_type>(parLeft[z] + parRight[z]);
#else
retval[z] = parLeft[z] + parRight[z];
#endif
}
return retval;
}
template <typename V1, typename V2>
inline Vec<typename std::common_type<V1, V2>::type> operator- (const Vec<V1>& parLeft, const Vec<V2>& parRight) {
typedef typename VectorWrapperInfo<typename std::common_type<V1, V2>::type>::scalar_type scalar_type;
static_assert(static_cast<int>(VectorWrapperInfo<V1>::dimensions) == static_cast<int>(VectorWrapperInfo<V2>::dimensions), "Dimensions mismatch");
Vec<typename std::common_type<V1, V2>::type> retval;
for (int z = 0; z < VectorWrapperInfo<V1>::dimensions; ++z) {
#if defined(VWR_STATIC_CAST_RESULTS)
retval[z] = static_cast<scalar_type>(parLeft[z] - parRight[z]);
#else
retval[z] = parLeft[z] - parRight[z];
#endif
}
return retval;
}
template <typename V1, typename V2>
inline Vec<typename std::common_type<V1, V2>::type> operator* (const Vec<V1>& parLeft, const Vec<V2>& parRight) {
typedef typename VectorWrapperInfo<typename std::common_type<V1, V2>::type>::scalar_type scalar_type;
static_assert(static_cast<int>(VectorWrapperInfo<V1>::dimensions) == static_cast<int>(VectorWrapperInfo<V2>::dimensions), "Dimensions mismatch");
Vec<typename std::common_type<V1, V2>::type> retval;
for (int z = 0; z < VectorWrapperInfo<V1>::dimensions; ++z) {
#if defined(VWR_STATIC_CAST_RESULTS)
retval[z] = static_cast<scalar_type>(parLeft[z] * parRight[z]);
#else
retval[z] = parLeft[z] * parRight[z];
#endif
}
return retval;
}
template <typename V1, typename V2>
inline Vec<typename std::common_type<V1, V2>::type> operator/ (const Vec<V1>& parLeft, const Vec<V2>& parRight) {
typedef typename VectorWrapperInfo<typename std::common_type<V1, V2>::type>::scalar_type scalar_type;
static_assert(static_cast<int>(VectorWrapperInfo<V1>::dimensions) == static_cast<int>(VectorWrapperInfo<V2>::dimensions), "Dimensions mismatch");
Vec<typename std::common_type<V1, V2>::type> retval;
for (int z = 0; z < VectorWrapperInfo<V1>::dimensions; ++z) {
#if defined(VWR_STATIC_CAST_RESULTS)
retval[z] = static_cast<scalar_type>(parLeft[z] / parRight[z]);
#else
retval[z] = parLeft[z] / parRight[z];
#endif
}
return retval;
}