A recent build of GCC 4.8 gives the following code, when in a header file:
auto L = [](){};
struct S
{
decltype(L) m;
};
the following warning:
test.hpp:3:8: warning: 'S' has a field 'S::m' whose type uses the anonymous namespace [enabled by default]
struct S
^
Why does the compiler consider the type of the lambda to use the anonymous namespace? I made the lambda global, I didn't use an anonymous namespace anywhere.
UPDATE: The compiles gives the same warning even if I put the lambda in an explicit namespace, like so:
namespace N
{
auto L = [](){};
}
struct S
{
decltype(N::L) m;
};
UPDATE 2: In fact, it seems even class scope lambdas have the same problem:
class N
{
static constexpr auto L = [](){};
};
struct S
{
decltype(N::L) m;
};
An anonymous namespace makes the enclosed variables, functions, classes, etc. available only inside that file. In your example it's a way to avoid global variables.
The Lambda service stores your function code in an internal S3 bucket that's private to your account. Each AWS account is allocated 75 GB of storage in each Region. Code storage includes the total storage used by both Lambda functions and layers.
We have seen that lambda is just a convenient way to write a functor, therefore we should always think about it as a functor when coding in C++. We should use lambdas where we can improve the readability of and simplify our code such as when writing callback functions.
Lambdas can both capture variables and accept input parameters. A parameter list (lambda declarator in the Standard syntax) is optional and in most aspects resembles the parameter list for a function. C++ Copy. auto y = [] (int first, int second) { return first + second; };
§5.1.2/3:
The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed nonunion class type — called the closure type — whose properties are described below. This class type is not an aggregate (8.5.1). The closure type is declared in the smallest block scope, class scope, or namespace scope that contains the corresponding lambda-expression.
So, unless you're defining the lambda expression in code inside an anonymous namespace, the lambda's type should not be contained in an anonymous namespace either.
Unless I missed something, none of these should be in an anonyous namespace, though atleast both GCC and MSVC seem to put them there.
§5.1.2 [expr.prim.lambda] p3
[...] The closure type is declared in the smallest block scope, class scope, or namespace scope that contains the corresponding lambda-expression. [...]
Atleast Clang seems to get it right, the closure type resides where it should be.
(You can test in which namespace a lambda type resides by simply containing the lambda in some kind of warning / error producing code. The compiler should spit out its type along with the warning / error.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With