daniel
daniel

Reputation: 439

Determining the type of a variable

You are given a variable p defined in C++ program. Is it possible to find out if it's a pointer to something or a normal variable? specifically, suppose there is a class Test

    class Test
    {
    public:
        int test1(int i)
        {
            
        }
        static int test2(int i)
        {
        }
const int test3(int i)
    {
    }

}

Now `this` is a variable accessible inside test1 and test2 and test3, But I want to determine the type of `this` inside each of these functions. It could be `Test,` `Test const,` `Test* const` etc...
How can I figure this out myself, or is there any alternate documentation for the same?

Upvotes: 0

Views: 97

Answers (1)

Bathsheba
Bathsheba

Reputation: 234635

You can use, having written #include<type_traits>

std::is_const<decltype(this)>::value

within a member function. If that member function is const then this value will be true, else it will be false.

Note there is no this in a static member function, and that this is always a pointer type in C++.

Upvotes: 1

Related Questions