Skip to content
Snippets Groups Projects
Commit aaaf2c14 authored by Markus Blatt's avatar Markus Blatt
Browse files

Statically check dimension of FieldVector in assignment.

[[Imported from SVN: r6494]]
parent f00cb61f
No related branches found
No related tags found
No related merge requests found
......@@ -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];
}
......
......@@ -66,3 +66,4 @@ shared_ptrtest_dune
tuplestest_dune
tuplestest_std
tuplestest_tr1
check_fvector_size
......@@ -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
......
// -*- 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;
}
// -*- 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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment