Bug 268820 - index C++ variable overloading
Summary: index C++ variable overloading
Status: Closed Works As Intended
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: 13.1-RELEASE
Hardware: Any Any
: --- Affects Many People
Assignee: freebsd-standards (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-01-07 21:15 UTC by cppleo
Modified: 2023-04-15 06:22 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description cppleo 2023-01-07 21:15:16 UTC
The STL <string> header require strings.h header. In this header you can see index variable. Variable can't be named without `_` in STL by C++ Standards. 
I can't compile my C++ project, because I use `index` variable too.

So, file `1.cpp`:
```
#include <string>

std::string index;

int main() {

}
```

Compile:
c++ 1.cpp


And I've got an error!
Comment 1 Mark Millard 2023-01-07 23:26:34 UTC
FYI: The specific error report is:

# c++ string_index_use.cpp 
string_index_use.cpp:3:13: error: redefinition of 'index' as different kind of symbol
std::string index;
            ^
/usr/include/strings.h:62:7: note: previous definition is here
char    *index(const char *, int) __pure;                       /* LEGACY */
         ^
1 error generated.

for:

# more string_index_use.cpp
#include <string>

std::string index;

int main() {

}

I get the same for stable/13 and main [so: 14].

For reference, for the __BEGIN_DECLS related use in strings.h :

# grep -r "define.*__BEGIN_DECLS" /usr/include/
/usr/include/sys/cdefs.h:#define	__BEGIN_DECLS	extern "C" {
/usr/include/sys/cdefs.h:#define	__BEGIN_DECLS

and the code block is:

#if defined(__cplusplus)
#define __BEGIN_DECLS   extern "C" {
#define __END_DECLS     }
#else
#define __BEGIN_DECLS
#define __END_DECLS
#endif

The strings.h is used via /usr/include/string.h by the text:

. . .
#include <sys/cdefs.h>
#include <sys/_null.h>
#include <sys/_types.h>

/*
 * Prototype functions which were historically defined in <string.h>, but
 * are required by POSIX to be prototyped in <strings.h>.
 */
#if __BSD_VISIBLE
#include <strings.h>
#endif
. . .

So __BSD_VISIBLE in involved in causing the extra definition
of index.
Comment 2 Mark Millard 2023-01-07 23:51:05 UTC
(In reply to Mark Millard from comment #1)

It looks like anything with __POSIX_VISIBLE==0 (such as adding
a line at the beginning to define _C11_SOURCE to avoid POSIX)
tends to get errors, such as:

In file included from string_index_use.cpp:5:
In file included from /usr/include/c++/v1/string:523:
In file included from /usr/include/c++/v1/__functional_base:23:
/usr/include/c++/v1/new:327:11: error: no member named 'posix_memalign' in the global namespace
  (void)::posix_memalign(&__result, __alignment, __size);
        ~~^
In file included from string_index_use.cpp:5:
In file included from /usr/include/c++/v1/string:525:
In file included from /usr/include/c++/v1/algorithm:667:
In file included from /usr/include/c++/v1/functional:506:
In file included from /usr/include/c++/v1/__functional/function.h:22:
In file included from /usr/include/c++/v1/__memory/shared_ptr.h:35:
In file included from /usr/include/c++/v1/atomic:524:
In file included from /usr/include/c++/v1/__thread/timed_backoff_policy.h:16:
/usr/include/c++/v1/__threading_support:411:11: error: use of undeclared identifier 'nanosleep'
   while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR);
          ^
2 errors generated.

__BSD_VISIBLE==0 is associated with __POSIX_VISIBLE==0 by
default. So it looks like __BSD_VISIBLE==0 is not really an
alternative for avoiding the issue that you hit.
Comment 3 Eugene Grosbein freebsd_committer freebsd_triage 2023-01-08 09:18:43 UTC
This works as designed. Use:
#define _POSIX_C_SOURCE 200809

And you will be fine.
Comment 4 Eugene Grosbein freebsd_committer freebsd_triage 2023-01-08 10:52:54 UTC
(In reply to Eugene Grosbein from comment #3)

Or, better:
#define _XOPEN_SOURCE 700
Comment 5 Eugene Grosbein freebsd_committer freebsd_triage 2023-04-15 06:22:01 UTC
Feedback timeout (3 months). That was pilot error.