从 type_id_with_cv<>()::p retty_name() 中删除命名空间
Remove namespace from type_id_with_cv<>()::pretty_name()
我使用以下代码来检索类的名称:
template <typename T>
string GetName(const T& object) {
using type = typename remove_const<typename remove_reference<decltype(object)>::type>::type;
return boost::typeindex::type_id_with_cvr<type>().pretty_name();
}
代码运行良好。但是,返回的字符串还包含命名空间。是否有仅返回类名的 boost 函数?我知道我可以自己写,关键是不要重新发明轮子。
这是轮子的另一项发明,基本上,它利用了命名空间结构和目录结构之间的相似性,但可怕的但快速的解决方案。
#include<boost/type_index.hpp>
#include<boost/filesystem/path.hpp>
namespace A{
namespace B{
struct C{};
}
}
int main(){
std::string s = boost::typeindex::type_id_with_cvr<A::B::C const*&>().pretty_name();
replace(begin(s), end(s), ':', '/');
replace(begin(s), end(s), ' ', '.');
using namespace boost::filesystem;
assert( path(s).string() == "A//B//C.const*&" );
assert( path(s).filename() == "C.const*&" );
assert( path(s).extension() == ".const*&" );
assert( path(s).stem() == "C" );
assert( path(s).parent_path() == "A//B");
assert( path(s).parent_path().parent_path() == "A");
}
或者把它打包成一个简单的丑陋类:
#include<boost/type_index.hpp>
#include<boost/filesystem/path.hpp>
namespace A{
namespace B{
struct C{};
}
}
template<class T>
class typename_info_t{
boost::filesystem::path p_;
public:
typename_info_t(){
std::string s = boost::typeindex::type_id_with_cvr<T>().pretty_name();
replace(begin(s), end(s), ':', '/');
replace(begin(s), end(s), ' ', '.');
p_ = s;
}
std::string full_name() const{
std::string s = p_.string();
replace(begin(s), end(s), '/', ':');
replace(begin(s), end(s), '.', ' ');
return s;
}
std::string namespace_name() const{
std::string s = p_.parent_path().string();
replace(begin(s), end(s), '/', ':');
return s;
}
std::string class_name() const{
std::string s = p_.stem().string();
replace(begin(s), end(s), '/', ':');
return s;
}
std::string class_name_with_cvr() const{
std::string s = p_.filename().string();
replace(begin(s), end(s), '/', ':');
replace(begin(s), end(s), '.', ' ');
return s;
}
std::string cvr() const{ // begins with space
std::string s = p_.extension().string();
replace(begin(s), end(s), '.', ' ');
return s;
}
};
template<class TT> typename_info_t<TT> typename_info(){return {};}
int main(){
assert( typename_info<A::B::C const*&>().full_name() == "A::B::C const*&" );
assert( typename_info<A::B::C const*&>().namespace_name() == "A::B" );
assert( typename_info<A::B::C const*&>().class_name() == "C" );
assert( typename_info<A::B::C const*&>().class_name_with_cvr() == "C const*&" );
assert( typename_info<A::B::C const*&>().cvr() == " const*&");
}
(限制:"*"指针限定符不是此处类名的一部分,不幸的是,这强制使用非仅标头库的#include<boost/filesystem.hpp>
或#include<experimental/filesystem>
。
相关文章:
- 命名空间中具有.h和.cpp文件的类
- 从父命名空间重载类型
- 在命名空间中定义函数还是限定函数
- C++:对不存在的命名空间使用命名空间指令
- 通过继承类使用来自不同命名空间的运算符
- 使用命名空间时出现多个定义错误
- CUDA内核和数学函数的显式命名空间
- 嵌套的匿名命名空间
- CMakeLists.txt中的命名空间表示法
- 类是C++中的命名空间吗
- 在命名空间中使用全局命名空间中的函数
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- '使用命名空间{嵌套在另一个命名空间中的某个命名空间}"
- 是否可以将函数导入命名空间,但不能导出它?
- C++ C++类中的命名空间降级
- 如何使用 soong 命名空间来有条件地编译模块
- 在 C++ 中使用命名空间时出现"does not name a type"错误
- 命名空间"命名空间"中没有名为"name"的成员
- 命名空间错误'Not a namespace-name'
- 在命名空间内实例化时的 C++ "Does not name type"