在lambda函数中捕获静态成员变量

时间:2018-10-18 07:03:46

标签: c++

我想知道是否可以在lambda函数内部捕获类的let parameters = [ "param1" : "1000", "param2": "Murat Akdeniz", "param3": "xxxxxx"] let imgData = UIImageJPEGRepresentation(UIImage(named: "1.png")!,1) Alamofire.upload( multipartFormData: { MultipartFormData in // multipartFormData.append(imageData, withName: "user", fileName: "user.jpg", mimeType: "image/jpeg") for (key, value) in parameters { let value = "\(value)" //Added this line to use [String:Any ] param types MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key) } MultipartFormData.append(UIImageJPEGRepresentation(UIImage(named: "1.png")!, 1)!, withName: "photos[1]", fileName: "swift_file.jpeg", mimeType: "image/jpeg") MultipartFormData.append(UIImageJPEGRepresentation(UIImage(named: "1.png")!, 1)!, withName: "photos[2]", fileName: "swift_file.jpeg", mimeType: "image/jpeg") }, to: "youURL") { (result) in switch result { case .success(let upload, _, _): upload.responseJSON { response in print(response.result.value) } case .failure(let encodingError): break print(encodingError) } } (lambda函数正在同一类的静态成员函数中使用)。

我一直在尝试以下操作,但无法编译代码:

static member variable

是否可以通过这种方式捕获静态成员变量?

修改后的代码(执行建议的更改后)

  #include<string>
  #include<iostream>

  using namespace std;

  class test_temp
  {
          public:
              static string name;
              static int count_of_letters();
  };

  string test_temp::name="Vishal";
  int test_temp::count_of_letters()
  {
        auto result = [&test_temp::name]() {return(test_temp::name.size());};

  }

  int main() {
  int res=test_temp::count_of_letters();
  cout<<endl<<res<<endl;
  }

1 个答案:

答案 0 :(得分:3)

静态存储变量(例如静态成员)无需捕获。只需删除捕获,它将起作用。您也不需要限定范围,因为您处于成员函数中。