Skip to content

Commit

Permalink
fix: calling hasInteraction() though an instance is not constexpr
Browse files Browse the repository at this point in the history
* clang identified the call to `hasInteraction()` as not being a
  constant expression.
* `Hamiltonian::hasInteraction()` being a `constexpr` static member
  function might not be a constant expression when invoked through an
  instance of a `Hamiltonian` object.
  Accessing it through the type by using `decltype` is guaranteed to
  work.
  • Loading branch information
Puerling committed Aug 25, 2024
1 parent f8c4a62 commit cdbd140
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions core/include/engine/common/Hamiltonian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,8 @@ class HamiltonianVariant : public TypeTraits
template<class T>
[[nodiscard]] bool hasInteraction()
{
return std::visit( []( auto & h ) { return h.template hasInteraction<T>(); }, hamiltonian );
return std::visit(
[]( auto & h ) { return std::decay_t<decltype( h )>::template hasInteraction<T>(); }, hamiltonian );
};

template<class T>
Expand All @@ -641,7 +642,7 @@ class HamiltonianVariant : public TypeTraits
return std::visit(
[]( const auto & h ) -> const typename T::Data *
{
if constexpr( h.template hasInteraction<T>() )
if constexpr( std::decay_t<decltype( h )>::template hasInteraction<T>() )
return &h.template data<T>();
else
return nullptr;
Expand All @@ -655,7 +656,7 @@ class HamiltonianVariant : public TypeTraits
return std::visit(
[]( const auto & h ) -> const typename T::Cache *
{
if constexpr( h.template hasInteraction<T>() )
if constexpr( std::decay_t<decltype( h )>::template hasInteraction<T>() )
return &h.template cache<T>();
else
return nullptr;
Expand All @@ -670,7 +671,7 @@ class HamiltonianVariant : public TypeTraits
[this,
data = typename T::Data( std::forward<Args>( args )... )]( auto & h ) mutable -> std::optional<std::string>
{
if constexpr( h.template hasInteraction<T>() )
if constexpr( std::decay_t<decltype( h )>::template hasInteraction<T>() )
return h.template set_data<T>( std::move( data ) );
else
return fmt::format(
Expand Down

0 comments on commit cdbd140

Please sign in to comment.