Skip to content

Commit

Permalink
Mechanism for better compile-time error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwbrei committed Oct 9, 2023
1 parent d11801b commit 38f13ef
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/libraries/JANA/Utils/JTypeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@

namespace JTypeInfo {


template <typename, typename=void>
struct is_parseable : std::false_type {};

template <typename T>
struct is_parseable<T, std::void_t<decltype(std::declval<std::istream>() >> std::declval<T&>())>> : std::true_type {};

template <typename, typename=void>
struct is_serializable : std::false_type {};

template <typename T>
struct is_serializable<T, std::void_t<decltype(std::declval<std::ostream>() << std::declval<T>())>> : std::true_type {};


template<typename T>
std::string demangle(void) {

Expand Down
31 changes: 31 additions & 0 deletions src/programs/tests/JParameterManagerTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,35 @@ TEST_CASE("JParameterManager_Issue217StringsWithWhitespace") {
}
}

#if __cplusplus >= 201703L
template <typename T>
void fakeParse(std::string s, T& out) {
constexpr bool parseable = JTypeInfo::is_parseable<T>::value;
static_assert(parseable, "Type is not automatically supported by the ParameterManager. To use, implement a template specialization for Parse(std::string in, T& out).");
if constexpr (parseable) {
std::stringstream ss;
ss >> out;
}
}
#else
template <typename T>
void fakeParse(std::string s, T& out) {
std::stringstream ss;
ss >> out;
}
#endif
}


enum class Mood {Good, Bad, Mediocre};
TEST_CASE("Error message on bad parse") {
int x;
fakeParse("22", x);
Mood m;
fakeParse("Mediocre", m);
}





0 comments on commit 38f13ef

Please sign in to comment.