Skip to content

Commit

Permalink
Fixed a problem when accessing a vector
Browse files Browse the repository at this point in the history
  • Loading branch information
straubar committed Feb 28, 2020
1 parent a7deb5c commit 79d2768
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions include/tpf/data/tpf_stencil.inl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@
#include <cmath>
#include <stdexcept>

namespace
{
template <typename U, typename T>
struct caster
{
template <typename U_ = typename std::remove_reference<U>::type, typename T_ = typename std::remove_reference<T>::type>
U& operator()(T& value, typename std::enable_if<std::is_convertible<T_, U_>::value>::type* = nullptr) const
{
return static_cast<U>(value);
}

template <typename U_ = typename std::remove_reference<U>::type, typename T_ = typename std::remove_reference<T>::type>
U operator()(T& value, typename std::enable_if<!std::is_convertible<T_, U_>::value>::type* = nullptr) const
{
return U(value.data());
}
};
}

namespace tpf
{
namespace data
Expand Down Expand Up @@ -235,13 +254,13 @@ namespace tpf
break;
case GRADIENT:
this->temp_constant = gradient(grid_coords);
return this->temp_constant;
return caster<return_type, element_type>()(this->temp_constant);

break;
case CONSTANT:
default:
this->temp_constant = this->constant;
return this->temp_constant;
return caster<return_type, element_type>()(this->temp_constant);
}
}
}
Expand Down

0 comments on commit 79d2768

Please sign in to comment.