c++ - template lambda sometimes doesn't compile -


i implement generic visitor pattern trie data structure. below extracted minimal fragment makes trouble compilers:

#include <functional>  struct node {     size_t length; };  template<typename n> class c { public:   size_t longest = 0;   std::function<void(const n )> f = [this](n node) {     if(node->length > this->longest) this->longest = node->length;   }; };  int main() {    node n;   n.length = 5;   c<node*> c;   c.f(&n); } 

it compiles g++ (ubuntu/linaro 4.7.2-2ubuntu1), ubuntu clang version 3.4-1ubuntu3 , apple llvm version 5.0 (clang-500.2.79). icc (icc) 14.0.2 says:

try_lambda_t.cc(15): error: "this" cannot used inside body of lambda   if(node->length > this->longest) this->longest = node->length; 

i found similar post: class non-static lambda member can't use default template paramers? story resulted in bug report resolved in g++ 4.8.1: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54764

however, g++ (ubuntu 4.8.2-19ubuntu1) results in:

internal compiler error: in tsubst_copy, @ cp/pt.c:12125    std::function<void(const n )> f = [this](n node) {                                         ^ please submit full bug report, preprocessed source if appropriate. 

what can have compiled newest g++ (and, hopefully, icc)?

gcc-4.8.1 compiles code if don't use non-static data member initializer initialize f

template<typename n> class c { public:   c()   : f([this](n node) {         if(node->length > longest) longest = node->length;       })   {}   size_t longest = 0;   std::function<void(const n )> f; }; 

live demo

it works if prefer referring longest this->longest within body of lambda.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -