-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Type.hpp to automatically deduce properties of the types at compile time. Type_class now contains only static members, and all except Type_class::getname() and Type_class::c_typename_to_id() are constexpr. The global variable Type still exists, but it is constexpr. Calls to Type.<something> are now equivalent to Type_class::<something>. But I do not suggest using Type_class directly, but instead consider removing the Type global variable and renaming Type_class to Type. It could also be a namespace rather than a class, which could be useful in some circumstances. I wasn't able to make Type_class::getname() constexpr because it returns a std::string. It *could* return a const char*, since all of the strings that it can return are string literals, but this causes a lot of compilation failures because of code like: cytnx_error_msg(dtype != Type.Float, "[ERROR] type mismatch. try to get <float> type from raw data of type %s", Type.getname(dtype).c_str()); where it is calling getname().c_str(), which obviously fails if getname() is returning a const char*. The proper fix is to improve cytnx_error_msg() so that it can print values in a type-safe way. After that is done, Type_class::getname() could be changed to constexpr and return a const char*. I wasn't able to make Type_class::c_typename_to_id() constexpr because typeid(T).name() is not constexpr. A possible extension would be to add a wrapper around typeid(T).name() that returns Type.getname() if T is a cytnx type, with a fallback if it isn't a cytnx type. It could also use some compiler-specific functions if available, for example both gcc and clang have abi::__cxa_demangle() function. MSVC users are out of luck ;)
- Loading branch information
Showing
2 changed files
with
201 additions
and
223 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
Oops, something went wrong.