小编Nik*_*ski的帖子

捕获的静态断言 lambda

我目前正在学习并行编程和元编程,并且我很难对捕获变量的 lambda 进行编译时检查。

假设我们有这样的代码:

#include <type_traits>
#include <functional>
#include <iostream>

int main(){

    auto non_capture = [](){
        std::cout<<"this lambda is not capturing!"<<std::endl;
    };

    auto capture = [&](){
        std::cout<<"this lambda is capturing"<<std::endl;
    };

    //value is true for this one
    static_assert(std::is_convertible<decltype(non_capture), std::function<void()>>::value, "message1");

    //i need help for this
    static_assert(is_lambda_that_captures<decltype(capture)>::value, "this is some error message");
}
Run Code Online (Sandbox Code Playgroud)

我需要帮助来定义is_lambda_that_captures类型特征,以便我可以传递第二个静态断言。我该怎么做?

拥有最新标准的解决方案是非常受欢迎的(当前最新的是 c++17):)

c++ c++17

0
推荐指数
1
解决办法
544
查看次数

标签 统计

c++ ×1

c++17 ×1