This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/17082] New: htons et al.: statement-expressions prevent use on global scope with -O1 and higher


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]