clean-up example
This commit is contained in:
parent
07e7d8dff6
commit
b95707b409
1 changed files with 8 additions and 6 deletions
|
@ -27,6 +27,10 @@
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
std::string operator+(std::string_view lhs, std::string_view rhs) {
|
||||||
|
return std::string{lhs.data(), lhs.length()}.append(rhs.data(), rhs.length());
|
||||||
|
}
|
||||||
|
|
||||||
constexpr long double operator"" _deg(long double deg) {
|
constexpr long double operator"" _deg(long double deg) {
|
||||||
return deg * 3.141592 / 180.0;
|
return deg * 3.141592 / 180.0;
|
||||||
}
|
}
|
||||||
|
@ -49,9 +53,7 @@ void SomeMethod3() {
|
||||||
|
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
std::string SomeMethod4(U value) {
|
std::string SomeMethod4(U value) {
|
||||||
std::stringstream s;
|
return NAMEOF(SomeMethod4<T, U>) + "<" + NAMEOF_TYPE(T) + ", " + NAMEOF_TYPE(U) + ">(" + NAMEOF_TYPE(U) + " " + NAMEOF(value) + ")";
|
||||||
s << NAMEOF(SomeMethod4<T, U>) << "<" << NAMEOF_TYPE(T) << ", " << NAMEOF_TYPE(U) << ">(" << NAMEOF_TYPE(U) << " " << NAMEOF(value) << ")";
|
|
||||||
return s.str();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
@ -135,15 +137,15 @@ int main() {
|
||||||
|
|
||||||
std::cout << SomeMethod4<int>(structvar) << std::endl; // 'SomeMethod4<int, SomeStruct>(SomeStruct value)'
|
std::cout << SomeMethod4<int>(structvar) << std::endl; // 'SomeMethod4<int, SomeStruct>(SomeStruct value)'
|
||||||
|
|
||||||
const auto div = [](int x, int y) -> int {
|
auto div = [](int x, int y) -> int {
|
||||||
if (y == 0) {
|
if (y == 0) {
|
||||||
throw std::invalid_argument(std::string(NAMEOF(y)).append(" should not be zero!"));
|
throw std::invalid_argument(NAMEOF(y) + " should not be zero!");
|
||||||
}
|
}
|
||||||
return x / y;
|
return x / y;
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const int z = div(10, 0);
|
auto z = div(10, 0);
|
||||||
std::cout << z << std::endl;
|
std::cout << z << std::endl;
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
std::cout << e.what() << std::endl; // 'y should not be zero!'
|
std::cout << e.what() << std::endl; // 'y should not be zero!'
|
||||||
|
|
Loading…
Reference in a new issue