有没有办法确定上下文是否允许使用“this”?

时间:2021-03-20 09:10:18

Is there some way to determine whether the context allows the use of "this"?

有没有办法确定上下文是否允许使用“this”?

My goal is write a generic macro, for logging, which depending on the context use "this" (for instance, for print the value of "this").

我的目标是编写一个通用宏,用于记录,这取决于上下文使用“this”(例如,用于打印“this”的值)。

2 个解决方案

#1


2  

Even if you could do this, you could never use it. Code must be legal even if it can never get invoked, and it wouldn't be legal to mention this in such a context. Consider:

即使你可以做到这一点,你也永远不会使用它。代码必须是合法的,即使它永远不会被调用,在这样的上下文中提及它是不合法的。考虑:

if (this_is_legal())
   std::cout << this << std::endl;
else
   std::cout << "not in member function" << std::endl;

Well, this code won't compile, even if the magic this_is_legal worked. Because the first std::cout line won't compile in a context where this is not legal.

好吧,即使魔法this_is_legal有效,这段代码也无法编译。因为第一个std :: cout行不会在不合法的上下文中编译。

You could do a very ugly const void *getThis() { return NULL; } as a global function and const void *getThis() { return this; } as a member function. That would give you a function that returns NULL or this. You have to hope, relying on undefined behavior, that the this pointer will be unmodified in a base class with no member variables.

你可以做一个非常丑陋的const void * getThis(){return NULL; }作为全局函数和const void * getThis(){return this;作为成员函数。那会给你一个返回NULL或者这个的函数。你必须希望,依赖于未定义的行为,这个指针将在没有成员变量的基类中被修改。

#2


1  

If you can afford to define a base class for debugging purposes then define global and a class member debug functions. The member function can use this while the global one can use other information and scoping rules can select the correct debug function.

如果您能够为调试目的定义基类,则定义全局和类成员调试函数。成员函数可以使用它,而全局函数可以使用其他信息,并且作用域规则可以选择正确的调试函数。

Another way is to define two macros:

另一种方法是定义两个宏:

#define ENTER_CLASS_SCOPE
# undef IN_CLASS
# define IN_CLASS 1

#define EXIT_CLASS_SCOPE
# undef IN_CLASS
# define IN_CLASS 0

and have the #define IN_CLASS 0 initially. Then you can use these macros at the top and end of cpp files defining member functions and check the flag in the DEBUG macro.

最初有#define IN_CLASS 0。然后,您可以在定义成员函数的cpp文件的顶部和末尾使用这些宏,并检查DEBUG宏中的标志。

#1


2  

Even if you could do this, you could never use it. Code must be legal even if it can never get invoked, and it wouldn't be legal to mention this in such a context. Consider:

即使你可以做到这一点,你也永远不会使用它。代码必须是合法的,即使它永远不会被调用,在这样的上下文中提及它是不合法的。考虑:

if (this_is_legal())
   std::cout << this << std::endl;
else
   std::cout << "not in member function" << std::endl;

Well, this code won't compile, even if the magic this_is_legal worked. Because the first std::cout line won't compile in a context where this is not legal.

好吧,即使魔法this_is_legal有效,这段代码也无法编译。因为第一个std :: cout行不会在不合法的上下文中编译。

You could do a very ugly const void *getThis() { return NULL; } as a global function and const void *getThis() { return this; } as a member function. That would give you a function that returns NULL or this. You have to hope, relying on undefined behavior, that the this pointer will be unmodified in a base class with no member variables.

你可以做一个非常丑陋的const void * getThis(){return NULL; }作为全局函数和const void * getThis(){return this;作为成员函数。那会给你一个返回NULL或者这个的函数。你必须希望,依赖于未定义的行为,这个指针将在没有成员变量的基类中被修改。

#2


1  

If you can afford to define a base class for debugging purposes then define global and a class member debug functions. The member function can use this while the global one can use other information and scoping rules can select the correct debug function.

如果您能够为调试目的定义基类,则定义全局和类成员调试函数。成员函数可以使用它,而全局函数可以使用其他信息,并且作用域规则可以选择正确的调试函数。

Another way is to define two macros:

另一种方法是定义两个宏:

#define ENTER_CLASS_SCOPE
# undef IN_CLASS
# define IN_CLASS 1

#define EXIT_CLASS_SCOPE
# undef IN_CLASS
# define IN_CLASS 0

and have the #define IN_CLASS 0 initially. Then you can use these macros at the top and end of cpp files defining member functions and check the flag in the DEBUG macro.

最初有#define IN_CLASS 0。然后,您可以在定义成员函数的cpp文件的顶部和末尾使用这些宏,并检查DEBUG宏中的标志。