[dune-functions] How to check that a function is scalar (or vector valued)

Carsten Gräser graeser at mi.fu-berlin.de
Wed Apr 1 21:54:04 CEST 2015


Hi,
I'm not sure if I understand the problem correctly.

Am 01.04.2015 um 10:10 schrieb Christian Engwer:
> Dear all,
> 
> I'm currently struggling with a particular problem. I tried to get the
> dune-pdelab interpolate work with new-style dune-functions
> functions. The particular problem for me is now, that I have to deduce
> the range type of my function. In particular I have check that the
> returned value is either is_scalar (we would like to be able to return
> double instead of FieldVector<double,1>) or that the size() is a
> constexpr with value 1. One particular problem is that the size method
> is not static, thus we can not easily check the value without an
> object... It seems my C++ skills are diminishing, so perhaps one you
> guys can enlighten me ;-)

Do you explicitly want to call the size() method? One can get an
"object" by doing a static_cast of nullptr to the desired type
(declval can't be used in a constexpr) and this even works
for simple types. But in view of this gcc error message it
seems that anything like this cannot work in general:

  getsize.cc: In function ‘int main()’:
  getsize.cc:32:29: error: call to non-constexpr function ‘constexpr int Foo<i>::size() const [with int i = 3]’
       auto bar = Foo<foo.size()+1>();
                               ^
  getsize.cc:16:19: note: ‘constexpr int Foo<i>::size() const [with int i = 3]’ is not usable as a constexpr function because:
       constexpr int size() const
                     ^
  getsize.cc:16:19: error: enclosing class of constexpr non-static member function ‘constexpr int Foo<i>::size() const [with int i = 3]’ is not a literal type
  getsize.cc:4:7: note: ‘Foo<3>’ is not literal because:
   class Foo
         ^
  getsize.cc:4:7: note:   ‘Foo<3>’ is not an aggregate, does not have a trivial default constructor, and has no constexpr constructor that is not a copy or move constructor
  getsize.cc:32:29: error: call to non-constexpr function ‘constexpr int Foo<i>::size() const [with int i = 3]’
       auto bar = Foo<foo.size()+1>();

> I tried to come up with a type trait to check for the value of size,
> but the non-static thing was always the show-stopper for me.

One way to get around would be to have a free size() method
which has to be specialized, see attached example. This is
not that strange: As far as I know there is even an accepted
proposal for std::size() in the C++17 draft.

Best,
Carsten


-------------- next part --------------
A non-text attachment was scrubbed...
Name: getsize.cc
Type: text/x-c++src
Size: 529 bytes
Desc: not available
URL: <https://lists.dune-project.org/pipermail/dune-functions/attachments/20150401/df596ee9/attachment.cc>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <https://lists.dune-project.org/pipermail/dune-functions/attachments/20150401/df596ee9/attachment.sig>


More information about the dune-functions mailing list