How to add an attribute to a lambda function?

3.4k Views Asked by At

Suppose I have a lambda

auto func = [](std::string msg) { throw std::runtime_error(msg); };

(admittedly, this example makes little sense, but that's not the point). If this were not a lambda, but an ordinary function, I would declare it with the noreturn attribute as in

[[noreturn]] void func(std::string msg) { throw std::runtime_error(msg); }

Can this also be done for a lambda? (I tried several variations with clang 3.5, but without any success.)


edit Using Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn), I tried

auto func = [](std::string msg) -> [[noreturn]] void { throw std::runtime_error(msg); };

or

auto func = [](std::string msg) [[noreturn]] { throw std::runtime_error(msg); };

but both were rejected. Is this an incompleteness/bug of clang 3.5?

1

There are 1 best solutions below

3
On

The following works in g++ 4.9.3:

void foo()
{
  auto func = [] (std::string msg) [[noreturn]] { throw std::runtime_error(msg); };
}

Works at http://ideone.com/49Ietf too.