diff --git a/dune/common/fvector.hh b/dune/common/fvector.hh index c61ea9cff21c28abeb95cf5ea742e2d58011f04d..92cc3fe87520676e732b2ec2f34fa4989d013d1e 100644 --- a/dune/common/fvector.hh +++ b/dune/common/fvector.hh @@ -52,6 +52,39 @@ namespace Dune { typedef typename FieldTraits<K>::real_type real_type; }; + /** + * @brief TMP to check the size of a DenseVectors statically, if possible. + * + * If the implementation type of C is a FieldVector, we statically check + * whether its dimension is SIZE. + * @tparam C The implementation of the other DenseVector + * @tparam SIZE The size we need assume. + */ + template<typename C, int SIZE> + struct IsFieldVectorSizeCorrect + { + enum { + /** + *@param True if C is not of type FieldVector or its dimension + * is not equal SIZE. + */ + value = true + }; + }; + + template<typename T, int SIZE> + struct IsFieldVectorSizeCorrect<FieldVector<T,SIZE>,SIZE> + { + enum {value = true}; + }; + + template<typename T, int SIZE, int SIZE1> + struct IsFieldVectorSizeCorrect<FieldVector<T,SIZE1>,SIZE> + { + enum {value = false}; + }; + + /** \brief vector space out of a tensor product of fields. * * \tparam K the field type (use float, double, complex, etc) @@ -95,6 +128,7 @@ namespace Dune { template<class C> FieldVector (const DenseVector<C> & x) { + dune_static_assert(((bool)IsFieldVectorSizeCorrect<C,SIZE>::value), "FieldVectors do not match in dimension!"); assert(x.size() == SIZE); for (size_type i = 0; i<SIZE; i++) _data[i] = x[i]; @@ -188,6 +222,7 @@ namespace Dune { template<class C> FieldVector (const DenseVector<C> & x) { + dune_static_assert(((bool)IsFieldVectorSizeCorrect<C,1>::value), "FieldVectors do not match in dimension!"); assert(x.size() == 1); _data = x[0]; } diff --git a/dune/common/test/.gitignore b/dune/common/test/.gitignore index 783468e96caf6504dbacf0e96b4a4e6852fd7957..5648cd0b9ee6d5290923779d1f1527c3d7a5c273 100644 --- a/dune/common/test/.gitignore +++ b/dune/common/test/.gitignore @@ -66,3 +66,4 @@ shared_ptrtest_dune tuplestest_dune tuplestest_std tuplestest_tr1 +check_fvector_size diff --git a/dune/common/test/Makefile.am b/dune/common/test/Makefile.am index 37cf316ee401888a402049c0eaddef2ab7edffc9..8fc39636e91f45449be375f05eab7780f4645726 100644 --- a/dune/common/test/Makefile.am +++ b/dune/common/test/Makefile.am @@ -6,6 +6,7 @@ TESTPROGS = \ arraytest \ bigunsignedinttest \ bitsetvectortest \ + check_fvector_size \ conversiontest \ deprtuplestest \ dynmatrixtest \ @@ -54,6 +55,8 @@ TESTPROGS = \ COMPILE_XFAIL=$(DUNE_COMMON_BIN)/xfail-compile-tests COMPILE_XFAIL_TESTS = \ + check_fvector_size_fail1 \ + check_fvector_size_fail2 \ genericiterator_compile_fail \ nullptr-test-fail \ static_assert_test_fail \ @@ -155,6 +158,14 @@ fmatrixtest_LDADD = $(LAPACK_LIBS) $(LDADD) fvectortest_SOURCES = fvectortest.cc +check_fvector_size_fail1_SOURCES = check_fvector_size_fail.cc +check_fvector_size_fail1_CPPFLAGS = -DDIM=1 + +check_fvector_size_fail2_SOURCES = check_fvector_size_fail.cc +check_fvector_size_fail2_CPPFLAGS = -DDIM=3 + +check_fvector_size_SOURCES = check_fvector_size.cc + poolallocatortest_SOURCES = poolallocatortest.cc enumsettest_SOURCES=enumsettest.cc diff --git a/dune/common/test/check_fvector_size.cc b/dune/common/test/check_fvector_size.cc new file mode 100644 index 0000000000000000000000000000000000000000..128cf11303d3a0338f79db393b29ac00396123ef --- /dev/null +++ b/dune/common/test/check_fvector_size.cc @@ -0,0 +1,15 @@ +// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- +// vi: set et ts=4 sw=2 sts=2: +#include <config.h> +#include <dune/common/dynvector.hh> + +int main(int argc, char * argv[]) +{ + + Dune::DynamicVector<double> one(1); + Dune::DynamicVector<double> two(2); + + two = one; + + return 0; +} diff --git a/dune/common/test/check_fvector_size_fail.cc b/dune/common/test/check_fvector_size_fail.cc new file mode 100644 index 0000000000000000000000000000000000000000..530d4f1c26218e94c68df60bfedd02b206942bd8 --- /dev/null +++ b/dune/common/test/check_fvector_size_fail.cc @@ -0,0 +1,15 @@ +// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- +// vi: set et ts=4 sw=2 sts=2: +#include <config.h> +#include <dune/common/fvector.hh> + +int main(int argc, char * argv[]) +{ + + Dune::FieldVector<double,DIM> one(1); + Dune::FieldVector<float,2> two(2); + + one=two; + + return 0; +}