-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tool to pretty-print type names
- Loading branch information
1 parent
cd119e9
commit 1161a94
Showing
4 changed files
with
52 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "type_name.hpp" | ||
#include <iostream> | ||
int | ||
main() | ||
{ | ||
std::cout << apsc::type_name<int>() << std::endl; | ||
std::cout << apsc::type_name<double>() << std::endl; | ||
double *x = new double[10]; | ||
std::cout << apsc::type_name<decltype(x)>() << std::endl; | ||
std::cout << apsc::type_name<std::string>() << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
// code based on | ||
// https://stackoverflow.com/questions/81870/is-it-possible-to-print-a-variables-type-in-standard-c | ||
#include <string_view> | ||
/*! | ||
@brief Get the type name of a variable | ||
@tparam T Type of the variable | ||
@return The type name of the variable | ||
@note possible usage: | ||
@code | ||
template <class T> | ||
void foo(const T& variable) | ||
{ | ||
std::cout << type_name<T>() << std::endl; | ||
} | ||
@endcode | ||
*/ | ||
namespace apsc | ||
{ | ||
template <class T> | ||
constexpr std::string_view | ||
type_name() | ||
{ | ||
#ifdef __clang__ | ||
std::string_view p = __PRETTY_FUNCTION__; | ||
return std::string_view(p.data() + 34, p.size() - 34 - 1); | ||
#elif defined(__GNUC__) | ||
std::string_view p = __PRETTY_FUNCTION__; | ||
#if __cplusplus < 201402 | ||
return std::string_view(p.data() + 36, p.size() - 36 - 1); | ||
#else | ||
return std::string_view(p.data() + 49, p.find(';', 49) - 49); | ||
#endif | ||
#endif | ||
} | ||
} // namespace apsc |