Skip to content

Commit

Permalink
DPL: provide cast to string_view for header::Descriptor<16>
Browse files Browse the repository at this point in the history
This will make some code compile under C++20, since C++20 is more aggressive in
doing implicit conversions where it currently uses the comparison operator.
  • Loading branch information
ktf committed Nov 7, 2023
1 parent 6ff3738 commit 07342ad
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions DataFormats/Headers/include/Headers/DataHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,21 @@ struct Descriptor {

// Note: don't need to define operator=(ItgType v) because the compiler
// can use Descriptor(ItgType initializer) for conversion
using ImplicitConversion = std::conditional_t<(size <= 8), ItgType, std::string_view>;

// type cast operator for simplified usage of the descriptor's integer member
// TODO: this is sort of a hack, takes the first element.
// we should rethink these implicit conversions
operator ItgType() const
// in case it does not fit into the descriptor, the string representation is returned
operator ImplicitConversion() const
{
static_assert(arraySize == 1, "casting Descriptor to ItgType only allowed for N<=8");
return itg[0];
if constexpr (std::is_same_v<ImplicitConversion, ItgType>) {
return itg[0];
} else {
size_t len = size;
while (len > 1 && str[len - 1] == 0) {
--len;
}
return std::string_view(str, len);
}
}

/// constructor from a compile-time string
Expand Down

0 comments on commit 07342ad

Please sign in to comment.