diff --git a/README.md b/README.md index d161675..842ae02 100644 --- a/README.md +++ b/README.md @@ -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(a+b). ### Automated conversion ### You can assign or construct a `Vec` from a `Vec`, 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. diff --git a/include/vectorwrapper/vectorwrapper.inl b/include/vectorwrapper/vectorwrapper.inl index 068e70a..1d2dca1 100644 --- a/include/vectorwrapper/vectorwrapper.inl +++ b/include/vectorwrapper/vectorwrapper.inl @@ -360,37 +360,57 @@ namespace vwr { template inline Vec::type> operator+ (const Vec& parLeft, const Vec& parRight) { + typedef typename VectorWrapperInfo::type>::scalar_type scalar_type; static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); Vec::type> retval; for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { +#if defined(VWR_STATIC_CAST_RESULTS) + retval[z] = static_cast(parLeft[z] + parRight[z]); +#else retval[z] = parLeft[z] + parRight[z]; +#endif } return retval; } template inline Vec::type> operator- (const Vec& parLeft, const Vec& parRight) { + typedef typename VectorWrapperInfo::type>::scalar_type scalar_type; static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); Vec::type> retval; for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { +#if defined(VWR_STATIC_CAST_RESULTS) + retval[z] = static_cast(parLeft[z] - parRight[z]); +#else retval[z] = parLeft[z] - parRight[z]; +#endif } return retval; } template inline Vec::type> operator* (const Vec& parLeft, const Vec& parRight) { + typedef typename VectorWrapperInfo::type>::scalar_type scalar_type; static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); Vec::type> retval; for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { +#if defined(VWR_STATIC_CAST_RESULTS) + retval[z] = static_cast(parLeft[z] * parRight[z]); +#else retval[z] = parLeft[z] * parRight[z]; +#endif } return retval; } template inline Vec::type> operator/ (const Vec& parLeft, const Vec& parRight) { + typedef typename VectorWrapperInfo::type>::scalar_type scalar_type; static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); Vec::type> retval; for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { +#if defined(VWR_STATIC_CAST_RESULTS) + retval[z] = static_cast(parLeft[z] / parRight[z]); +#else retval[z] = parLeft[z] / parRight[z]; +#endif } return retval; }