2013-10-07 2 views
0

API를 만들려고 할 때 MiniGW에 문제가 있습니다. 이것은 MSVC11 (Visual Studio 2012 C++ 컴파일러)에서 잘 작동합니다. 내 컴파일러는 (나는) 올바르게 λ3 오류 메시지를 가지고 결국 QMAKE_CXXFLAGS += -std=c++0x, 설정됩니다 믿습니다. 좀 더 설명은람다를 std :: function으로 변환하는 데 문제가 있습니다.

:

typedef std::function<bool(Item)> WHERE;//I was attempting to provide an explicit cast, 'auto' works fine in MSVS

Group where(WHERE func);//A simple "foreach boolean" search

에서 전술>

Groups::WHERE where = [](Item & x)-> bool{return x.has("NEW");};

결과를 사용 :

tst_groupstest.cpp:257: error: conversion from 'GroupsTest::groups()::__lambda1' to non-scalar type 'Groups::WHERE {aka std::function}' requested Groups::WHERE where = [](Item & x)-> bool{return x.has("NEW");};

나는 이것이 분명히 밝혀지기를 바랄뿐입니다. 이 프로젝트로 Linux와 Mac을 지원할 계획이므로 나중에이 파일을 빨리 이해하고 싶습니다.

여기에 현재의 해결 방법이 있습니다. 가능한 모든 경우 (람다를 사용하는 API를 설계 한 이유는 명백한 간결한 코드 블록을 사용하는 것이기 때문에)를 멀리하고 싶습니다.

이 섹션 컴파일 (는 람다를 사용하지 않는)

struct lambdaCueListstdFunc{ 
    bool operator()(Groups::Item x) 
    { 
     return x.has("NEW"); 
    } 
}; 

/** 
* Selects all cues for a particular list 
* @brief getCueList 
* @param list 
* @return a vector of all the cues for this list sorted by number. 
*/ 
std::vector<Groups::Item> CueService::getCueList(std::string list) 
{ 
    std::function<bool(Groups::Item)> where = lambdaCueListstdFunc(); 
// auto where = [&list] (Groups::Item & x) -> 
// { 
//  return x.get(la::cues::List) == list; 
// }; 
    std::vector<Groups::Item> result = cues()->where(where).sort(la::cues::NUMBER); 
    return result; 

} 

답변

5

당신이 std::function 제공 함수 서명이 당신이 그것을에 할당하려는 람다 다르다.

typedef std::function<bool(Item)> WHERE; // <-- Takes argument by value 

Groups::WHERE where = [](Item & x)-> bool{return x.has("NEW");}; 
//       ^^^ 
// The lambda takes argument by reference 

값 또는 참조로 전달할 것인지 여부에 따라 둘 중 하나가 변경됩니다.

+0

감사를 통과, 당신은 하나를 알아 내기 위해 노력하는 나에게 몇 시간을 저장. –

0

이것은 다른 이유로 나왔습니다. 람다 args를 통해 전달 된 by-value 형식은 std :: unique_ptr 형식의 멤버를 가지므로 복사 할 수 없습니다.

수정 1 : 변경 회원은 수 std 될 :: shared_ptr의

수정 2 : (CONST) 참조

관련 문제