1

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;
        }
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
VeNToR
  • 57
  • 1
  • 4
  • 4
    `consteval` must be evalued at compile time, but `typeid` is known only at runtime, therefore use `constexpr` instead. – metablaster Mar 29 '23 at 21:43
  • @metablaster, sure... but I need to use consteval for performance. – VeNToR Mar 29 '23 at 21:48
  • It's impossible to evalueate `typeid` operator at compile time. – metablaster Mar 29 '23 at 21:51
  • @metablaster thanks. any other way ? – VeNToR Mar 29 '23 at 21:52
  • 3
    Notes: 1) Any time you need to know the true type of a variable art runtime you've already pretty much lost your shot at good performance. 2) The information you get from `name` is very loosely specified. [Quoting cppreference](https://en.cppreference.com/w/cpp/types/type_info/name): "No guarantees are given; in particular, the returned string can be identical for several types and change between invocations of the same program." There aren't many good uses for `name`. – user4581301 Mar 29 '23 at 22:04
  • 1
    @metablaster `typeid` itself is allowed here in at compile-time. It is only the `name` member function which is not marked `constexpr` causing a problem. And with C++23 `operator==` of `std::type_info` is even `constexpr`, so `typeid` isn't completely useless in the context at least. – user17732522 Mar 29 '23 at 23:59

2 Answers2

1

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.

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
0

If you want to evaluate the class name at compile time, declare a static variable consisting of a predefined name, as follows:

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

        // ...
        // ...

        return ChReturn;
    }

private:
    static constexpr const char* name = "CChTest";
};

int main()
{
    CChTest test;
    std::cout << test.test();

    return 0;
}

This means you'll have to maintain class names yourself.

To make it work with inheritance, let each derived class override the method, which means you need to make it virtual consteval.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
metablaster
  • 1,958
  • 12
  • 26