c++ - Lambda 转换函数指针比较

标签 c++ lambda language-lawyer

简单地说,下面的断言会触发吗?

template<typename T>
auto destructor()
{
    return +[](void* p){
        ((T*)p)->~T();
    };
}

assert(destructor<int>() != destructor<char>());

标准似乎只说 lambda converted function pointer does the same thing as the lambda itself ,然后您会意识到普通的析构类型都具有无操作析构函数,因此是相同的。

最佳答案

[expr.prim.lambda]/6 :

The value returned by this conversion function is the address of a function F that, when invoked, has the same effect as invoking the closure type's function call operator.

指向的函数是根据其行为指定的,而不是根据其身份指定的。因此未指定此断言是否会触发。

关于c++ - Lambda 转换函数指针比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48485229/

相关文章:

c++ - 为什么依赖的应用程序要使用与动态库相同的编译器?

c++ - 使用声明和歧义声明的上下文

c# - 从具有未知签名的 MethodInfo 创建表达式函数

c# - 将 .net Func<T> 转换为 .net Expression<Func<T>>

c++ - istream 读取失败时存储的值

c++ - 为什么重载 operator new 会改变 new[] 的行为?

c++ - 状态模式 C++

c++ - 取消引用的 InputIterator 的地址? istream_iterator 的案例

c++ - boost:uuid 到 char * 没有 std::string

c++ - 我们什么时候可以省略 C++11 lambda 中的返回类型?