从type_id_with_cv <>():: pretty_name()中删除命名空间

Mav*_*rik 5 c++ boost typeid

我正在使用以下代码来检索类的名称:

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();

}
Run Code Online (Sandbox Code Playgroud)

代码效果很好.但是,返回的字符串也包含名称空间.是否有一个只返回类名的boost函数?我知道我自己可以写它,关键是不要重新发明轮子.

alf*_*lfC 0

这是轮子的另一个发明,也是可怕但快速的解决方案,基本上,利用了命名空间结构和目录结构之间的相似性。

#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");
}
Run Code Online (Sandbox Code Playgroud)

或者将其封装成一个简单的丑陋的类:

#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*&");

}
Run Code Online (Sandbox Code Playgroud)

(限制:“*”指针限定符不是此处类名的一部分,不幸的是,这强制使用#include<boost/filesystem.hpp>#include<experimental/filesystem>哪些是非仅头文件库。