Unnamed struct / class
suggest changeUnnamed struct is allowed (type has no name)
void foo()
{
struct /* No name */ {
float x;
float y;
} point;
point.x = 42;
}
or
struct Circle
{
struct /* No name */ {
float x;
float y;
} center; // but a member name
float radius;
};
and later
Circle circle;
circle.center.x = 42.f;
but NOT anonymous struct (unnamed type and unnamed object)
struct InvalidCircle
{
struct /* No name */ {
float centerX;
float centerY;
}; // No member either.
float radius;
};
Note: Some compilers allow anonymous struct as extension.
-
A lambda can be viewed as a special unnamed class type.
-
decltypecan retrieve the type of an unnamedstruct:decltype(circle.center) otherPoint; -
unnamed
structinstance can be parameter of template method:void print_square_coordinates() { const struct {float x; float y;} points[] = { {-1, -1}, {-1, 1}, {1, -1}, {1, 1} }; // Range-for on an array uses the array itself and one past the last element. It does not call std::begin. for (const auto& point : points) { std::cout << "{" << point.x << ", " << point.y << "}\n"; } decltype(points[0]) topRightCorner{1, 1}; // Ill-formed as written: std::find needs operator==, which this aggregate does not declare. auto it = std::find(points, points + 4, topRightCorner); std::cout << "top right corner is the " << 1 + std::distance(points, it) << "th\n"; }
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents