public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jh_sourceware.org at sotun dot de" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug libc/17082] New: htons et al.: statement-expressions prevent use on global scope with -O1 and higher
Date: Mon, 23 Jun 2014 19:53:00 -0000	[thread overview]
Message-ID: <bug-17082-131@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=17082

            Bug ID: 17082
           Summary: htons et al.: statement-expressions prevent use on
                    global scope with -O1 and higher
           Product: glibc
           Version: unspecified
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: jh_sourceware.org at sotun dot de
                CC: drepper.fsp at gmail dot com

htons() and many other libc functions are implemented using macros with
statement-expressions when compiled with -O1 or higher. Statement-expressions
are not supported on global scope in GCC, resulting in a compilation failure of
code like

> cat test.cpp
#include <netinet/in.h>
in_port_t p = htons(42);
> g++ test.cpp -O1
test.cpp:2:15: error: statement-expressions are not allowed outside functions
nor in template-argument lists

This seems to be a design decision for a manual optimization; and against
supporting the rare case of complex code on global scope.

It may be time to reevaluate that decision:
1. Small functions can be reliably inlined, optimizing away instructions that
otherwise would have been prevented using macros.
2. C++ lambda functions are on the rise. They allowing several nice patterns
with global scope code (e.g. for data structure initialization). But
statement-expressions do not work in lambdas defined on global scope. Their
prevalence in glibc complicates said patterns and causes confusing error
messages when moving code between function and global scope.

Implementing user-visible libc functions as functions instead of macros solves
this problem.

-- 
You are receiving this mail because:
You are on the CC list for the bug.


             reply	other threads:[~2014-06-23 19:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-23 19:53 jh_sourceware.org at sotun dot de [this message]
2014-06-23 20:10 ` [Bug libc/17082] " joseph at codesourcery dot com
2014-06-24  7:40 ` fweimer at redhat dot com
2015-03-15  7:59 ` cemkalyoncu at gmail dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-17082-131@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).