简体   繁体   English

以下代码如何为唯一的调用堆栈每次都唯一地实例化模板函数?

[英]How does the following code work to uniquely instantiate a template function everytime for a unique call-stack?

I've encountered the following code from Unreal Engine 's source我遇到了以下来自Unreal Engine源代码的代码

namespace UE4Asserts_Private
{
    // This is used by ensure to generate a bool per instance
    // by passing a lambda which will uniquely instantiate the template.
    template <typename Type>
    bool TrueOnFirstCallOnly(const Type&)
    {
        static bool bValue = true;
        bool Result = bValue;
        bValue = false;
        return Result;
    }

    FORCEINLINE bool OptionallyDebugBreakAndPromptForRemoteReturningFalse(bool bBreak, bool bIsEnsure = false)
    {
        if (bBreak)
        {
            FPlatformMisc::DebugBreakAndPromptForRemoteReturningFalse(bIsEnsure);
        }
        return false;
    }
}

#define ensure(           InExpression                ) (LIKELY(!!(InExpression)) || FDebug::OptionallyLogFormattedEnsureMessageReturningFalse(UE4Asserts_Private::TrueOnFirstCallOnly([]{}), #InExpression, __FILE__, __LINE__, TEXT("")               ) || UE4Asserts_Private::OptionallyDebugBreakAndPromptForRemoteReturningFalse(UE4Asserts_Private::TrueOnFirstCallOnly([]{}), true))

Now, whenever we use ensure(SomeExpression) , the UE4Asserts_Private::TrueFirstCallOnly argument to FDebug::OptionallyLogFormattedEnsureMessageReturningFalse evaluates to true only on the first time it gets called for a particular callstack ( I'm thinking per callstack, as TrueOnFirstCallOnly evaluates to false on the next call to ensure from the same callstack, but triggers the ensure from a different callstack but not very sure ) and I don't understand how this works.现在,每当我们使用ensure(SomeExpression)UE4Asserts_Private::TrueFirstCallOnly参数FDebug::OptionallyLogFormattedEnsureMessageReturningFalse计算结果为真实的,只有在第一次被调用特定的调用堆栈(我每次调用堆栈想,作为TrueOnFirstCallOnly计算结果为假的下一次调用来自同一调用堆栈的确保,但从不同的调用堆栈触发确保但不是很确定),我不明白这是如何工作的。

As they state in the comments, Somehow passing the lambda []{} to the template function uniquely instantiates it.正如他们在评论中所述,以某种方式将 lambda []{}传递给模板函数会唯一地实例化它。 How does it work?它是如何工作的? And what is the lambda passed as a template really unique for, is it the call-stack or something else?什么是作为模板传递的 lambda 真正独特的,它是调用堆栈还是其他什么?

LIKELY(!!(InExpression)) can be just thought to evaluate to true if the expression is true如果表达式为真,则LIKELY(!!(InExpression))可以被认为评估为真

This is how such a true_on_first_call could be implemented:这就是如何实现这样的true_on_first_call

include <iostream>

template <typename T> struct true_on_first_call {
    static bool first;
    bool operator()() {
        if (first) {
            first = false;
            return true;
        }
        return false;
    }
};
template <typename T> bool true_on_first_call<T>::first = true;
template <typename T> 
bool get_true_on_first_call(const T &){ return true_on_first_call<T>()(); }

void foo() {
    std::cout << get_true_on_first_call([]{}) << "\n";    // instantiation for []{}
}
void bar() {
    std::cout << get_true_on_first_call([]{}) << "\n";    // instantiation for []{}
}                                                         // note: its a different type 
                                                          // than the []{} above!
                                                          // but the same on 
                                                          // repeated calls to foo


int main() {
    std::cout << "first \n";
    foo();
    bar();
    std::cout << "second \n";
    foo();
    bar();
}

Live demo现场演示

The trick is that each labmda expression has a unique type, hence it will result in a different instantiation of true_on_first_call .诀窍是每个 labmda 表达式都有一个唯一的类型,因此它会导致true_on_first_call的不同实例化。 Even if the lambdas expressions are the same ( []{} vs []{} ) they are of different type.即使 lambdas 表达式相同( []{}[]{} ),它们的类型也不同。 On the other hand, the same lambda expression (ie the one on first call of foo and the one on second call of foo ) are of same type.另一方面,相同的 lambda 表达式(即第一次调用foo表达式和第二次调用foo表达式)是相同的类型。 In this way one can get a unique instantiation each time you write get_true_on_first_call([]{}) .通过这种方式,您每次编写get_true_on_first_call([]{})时都可以获得唯一的实例化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM