Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

when would one use a structure with an omitted tag

51 views
Skip to first unread message

G G

unread,
Jun 8, 2019, 8:35:37 PM6/8/19
to

p. 146 C++ Primer Plus, Stephen Prata

example:

struct // no tag
{
int x; // 2 members
int y;
} position; // a structure variable

when would something where the tag is omitted be useful?

Juha Nieminen

unread,
Jun 9, 2019, 6:29:29 AM6/9/19
to
"struct { something }" acts pretty much as a type, and can be
used anywhere where a type could be used. You don't have to create
a type alias for it if you don't want to. Thus code like this is
perfectly valid:

struct { int a, b; } values = { 1, 2 };
std::cout << values.a << " " << values.b << "\n";

Now, whether that's extraordinarily *useful*... I can't think of
any situation where it would be particularly useful over a named
struct type. Maybe in extremely simple situations like the above,
where you simply want to group a number of values into a "packet"
in that manner, and not create a new name for it. But even then,
you aren't saving much, really.

Sam

unread,
Jun 9, 2019, 7:46:50 AM6/9/19
to
Mostly when someone is coding in C, but there are some occasions where a
structure is used only once, for a very specific purpose, within a function
scope, and it serves no point to declare the structure formally. Such as,
using an example from real C++ code, and not a contrived example from a
garbage C++ book:

static const struct {
const char *name;
font &(font::*handler)(double);
} double_values[]={
{ "point_size", &font::set_point_size},
{ "scaled_size", &font::set_scaled_size},
{ "scale", &font::scale},
};

for (const auto &v:double_values)
{
// ...

Need a mapping between symbolic labels and class methods here. Simply
declare an anonymous struct and iterate over it, doing what needs to be done.

No need to formally declare this, in some header file, somewhere, when this
is needed just here and nowhere else.

P.S., it's not a "tag", but a class name. Given the other garbage example
you showed from this so-called "C++ book", you might consider returning it
for a refund and getting a better C++ book to learn from. C++ is hard enough
as it is.


james...@alumni.caltech.edu

unread,
Jun 9, 2019, 4:30:51 PM6/9/19
to
It is never useful to omit the tag, but it can often be useless to
provide one. Specifically, try writing the code that makes use of the
struct without providing a tag. If you can do so, then you don't need
the tag. This isn't commonplace, but neither is it extremely rare.

There's minor advantages to not providing a tag: you don't have to come
up with a name for it, and since it isn't named, it can't conflict with
anything else that happens to have the same name with a different
meaning. I don't go out of my way to write such code, but if I happen to
notice that the tag is not needed, I will often leave it out.

Jorgen Grahn

unread,
Jun 10, 2019, 10:01:35 AM6/10/19
to
Sometimes, if I have a struct or class with too many members,
I can group some of them in such a struct.

class Foo {
// lots of stuff

std::function<void()> foo_callback;
std::function<void()> bar_callback;
std::function<void()> baz_callback;
};

class Foo {
// lots of stuff

struct {
std::function<void()> foo;
std::function<void()> bar;
std::function<void()> baz;
} callback;
};

Works better if foo, bar and baz are default-constructed,
so callbacks are perhaps not a good example ... a better
one would be a bunch of statistics counters.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Tim Rentsch

unread,
Jun 20, 2019, 1:19:28 AM6/20/19
to
james...@alumni.caltech.edu writes:

> On Saturday, June 8, 2019 at 8:35:37 PM UTC-4, G G wrote:
>
>> p. 146 C++ Primer Plus, Stephen Prata
>>
>> example:
>>
>> struct // no tag
>> {
>> int x; // 2 members
>> int y;
>> } position; // a structure variable
>>
>> when would something where the tag is omitted be useful?
>
> It is never useful to omit the tag, [...]

In C it can be useful to omit a tag, if one wants to ensure that
the type is not used anywhere else in the program, or simply make
it evident that the type will not be used anywhere else in the
program.

That reasoning doesn't apply in C++, because of decltype.

G G

unread,
Jun 21, 2019, 5:37:31 PM6/21/19
to

> > It is never useful to omit the tag, [...]
>
> In C it can be useful to omit a tag, if one wants to ensure that
> the type is not used anywhere else in the program, or simply make
> it evident that the type will not be used anywhere else in the
> program.
>
> That reasoning doesn't apply in C++, because of decltype.

but then too isn't it, well for the situation you gave, a plus in C++,
cause of objects, data members and member functions could be made private.

Tim Rentsch

unread,
Jun 22, 2019, 6:35:01 AM6/22/19
to
My point was only about the properties of omitted tags,
nothing more.
0 new messages