Skip to content
Snippets Groups Projects

MultiTypeBlockVector: Use std::common_type for field_type

This change should make the field_type more meaningful. As far as I see it should not break any application.

If this is fine I'll add something similar to `MultiTypeBlockMatrix".

Merge request reports

Merged by Ansgar BurchardtAnsgar Burchardt 3 years ago (Dec 14, 2021 2:21pm UTC)

Loading

Pipeline #42160 passed

Pipeline passed for dcd3dce2 on master

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Patrick Jaap added 1 commit

    added 1 commit

    • baca4efc - Replace std::vector by Dune::DynamicVector in a unit test

    Compare with previous version

    • Resolved by Patrick Jaap

      I like the common_type approach, but I'm wondering about the exact semantics. We should document how it works.

      What is the field_type of e.g. MultiTypeBlockVector<DynamicVector<FieldVector<double,3>>,DynamicVector<FieldVector<float,1>>>?

      Do we get a meaningful type? If not, we should ensure that we get a static error.

  • Patrick Jaap added 2 commits

    added 2 commits

    • e5bdddbb - MultiTypeBlockVector: Use std::common_type for field_type
    • 86dda7dc - Replace std::vector by Dune::DynamicVector in a unit test

    Compare with previous version

  • Author Contributor

    @christi I added a test for some simple double and float combinations.

  • Patrick Jaap added 7 commits

    added 7 commits

    • 86dda7dc...576775c4 - 4 commits from branch core:master
    • 5e4d9dbc - MultiTypeBlockVector: Use std::common_type for field_type
    • 876a5440 - Replace std::vector by Dune::DynamicVector in a unit test
    • fa4cbd1d - MultiTypeBlockVector: Add static error message if no std::common_type exists

    Compare with previous version

  • Patrick Jaap added 1 commit

    added 1 commit

    • 00674d8e - MultiTypeBlockVector: Add static error message if no std::common_type exists

    Compare with previous version

  • Patrick Jaap added 1 commit

    added 1 commit

    • de10df78 - MultiTypeBlockVector: Add static error message if no std::common_type exists

    Compare with previous version

  • Patrick Jaap added 1 commit

    added 1 commit

    • fb8d4fd7 - MultiTypeBlockVector: Add static error message if no std::common_type exists

    Compare with previous version

  • 109 * for an empty tuple.
    110 * We have to wrap `double` by `std::decay` in order to call `::type` in the end,
    111 * since an empty `std::common_type` has no type member and the construction of
    112 * `std::conditional_t`would fail.
    113 * We use the helper struct `Impl_::CommonTypeOrNoCommonType` to set the dummy `NoCommonType`
    114 * for a more readable error message in case of a missing `std::common_type` implementation.
    80 115 */
    81 typedef double field_type;
    116 using field_type = typename std::conditional_t< sizeof...(Args) == 0,
    117 std::decay<double>, // in C++20 std::type_identity is preferrable
    118 Impl_::CommonTypeOrNoCommonType< typename std::decay_t<Args>::field_type... >
    119 >::type;
    120
    121 // make sure that we have an std::common_type: using an assertion produces a more readable error message
    122 // than a compiler template instatiation error
    123 static_assert ( not std::is_same_v<field_type, Impl_::NoCommonType>,
    • If you trigger a static_assertion anyway, I don't see any reason to define NoCommonType and CommonTypeEnabled. This could be replaced by

      static_assert(Dune::is_detected_v<std::common_type_t, T...>, ...);

      If you want to represent that there is no common type, you can use

      using field_type = Dune::detected_t<std::common_type_t, T...>;

      which is either std::common_type_t<T...> or Dune::nonesuch.

      Both rely on (Dune-implementation of) the detected mechanism from TSv2, and are thus probably more idiomatic.

      Edited by Carsten Gräser
    • And if you want a custom fallback type:

      using field_type = Dune::detected_or_t<NoCommonType, std::common_type_t, T...>;
    • That's a nice solution

    • Author Contributor

      Yes this looks indeed nice. how should we deal with the sizeof...(Args) == 0 case? Should I leave the std::conditional_t in or should be specialize an empty class MultiTypeBlockVector<>?

    • I would add a static_assert(sifeof...(Args) >0) unless there is a good reason to support this. Otherwise, the proposed solution would simply detect the fallback type, which is IMO still the best solution for an empty matrix.

    • changed this line in version 8 of the diff

    • Author Contributor

      Ok, I updated the MR and included the Std::detected_t solution. While adding the static_assert(sifeof...(Args) >0) assertion I got errors in almost all unit tests that the assertion fails. I really don't know why since no empty vectors are created.

    • Please register or sign in to reply
  • Patrick Jaap added 1 commit

    added 1 commit

    • b552a3ac - MultiTypeBlockVector: Add static error message if no std::common_type exists

    Compare with previous version

  • Patrick Jaap added 1 commit

    added 1 commit

    • cb6aea69 - MultiTypeBlockVector: adapt FieldTraits to the new field_type

    Compare with previous version

  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Please register or sign in to reply
    Loading