Search code examples
c++visual-c++consteval

typeid().name() and consteval function problem


I am using MSVC 19, and get an error while compiling the following code. Is there any way to get type_info and name of the object in consteval functions?

class CChTest
{
    public:
        static consteval const char*    test()
        {
            const char* ChReturn = typeid(CChTest).name();

            // ...
            // ...

            return ChReturn;
        }
};

Solution

  • If you want to get class name at compile time there is no standard solution yet.

    The Boost Type Index library does the job very well for over a decade. Its header only, you just need to include the boost directory containing the headers to use it in your project.

    Its build on to of macros like __PRETTY_FUNCTION__, __FUNCTION__ and __func__. You can use these directly, but it depends on your compiler and even the specific compiler version, at what position in the string the type name is. So its better to let a very well tested and maintained library to do the job.