delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2019/01/06/20:19:49

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:mime-version:from:date:message-id:subject:to
:cc:content-type; q=dns; s=default; b=UxJl8jRy1Lp+FW35I9emq1Fl4d
pvb6ggDON5r6ET7/YmQntsPEoE+FHVefJBrm5SMwnl3voYP7ekZQR2YWFQVi48E2
o0igmfVeeZssOfCDqakajypTC49s4tpNyk48+agOFz7pl95UenExS2K0Y5MacJS7
2bENsiOmBp8HKWLHA=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:mime-version:from:date:message-id:subject:to
:cc:content-type; s=default; bh=Mm/W8o7aGWN3HAhCRRR12F8JWvI=; b=
M/SdDzD4Ytfl9gG+bisNl3iw/6DPS+UfQmU5cjbIa9B2/dah7CfDOrXXAkqdyfLI
aPbHiZY9ld1tCAJFMFEnWKOl36LhztxOBRxvh+BeW5bL62VAh2Z1KLMyKxTKgu3q
WnFga61UdcdRPnXDLBEO8YPBkP6eSM8Ov+2FptOe+NI=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=arrange, HX-HELO:sk:mail-ed, sk:314159, visible
X-HELO: mail-ed1-f52.google.com
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to:cc; bh=6fu1S+WY84NJevsHK2CaOit6gPF1X68wQcFSXjRMeso=; b=bF83UCMejmBehAtmIdHDvBwcc5Z2i0By4feLeobjmGEoqA1EFYQc/EncRaNn6n5dcU XQeRIaHyCC3hK4jZtRbiITLebohYo+fyA0ezNtXXoTNGj6uuSKMKk1BhUuMM/WItTPHj HL7Vh25ZV16QFB3t99dBo840rm1xbcSKZq+ESp94bKi+BhUVxz9B4MXtkbLz2pWuej1H PO3snAUYOoc3RDDkC8fxrn6z9oIHNJ/3fyZcPS3u35psWv2IQcP/m5tfQNEIwyx2JlzA rsmBpxpiV2dd6ok35odbA7wj/9oZgc6u6LcPvpvOlEl5nNblh2MdUckmQc8W+CRcaYM/ 6HNQ==
MIME-Version: 1.0
From: Keith Thompson <keithsthompson AT gmail DOT com>
Date: Sun, 6 Jan 2019 17:19:25 -0800
Message-ID: <CAAHpriOFzks092zT+chJw4Dd91rPsO0mR3wO1gue8btARmBHKw@mail.gmail.com>
Subject: clang++: M_PI is visible in conforming mode
To: cygwin AT cygwin DOT com
Cc: Keith Thompson <Keith DOT S DOT Thompson AT gmail DOT com>

The macro M_PI, which expands to a constant approximating the value
of pi, is defined by POSIX, but not by ISO C or ISO C++.  It is
not a reserved identifier, so it should be available for use as a
user-defined identifier.

The problem: Including <cmath> causes M_PI to be defined, even when
the C++ compiler is invoked in what should be a conforming mode.

This problem occurs with clang++.  It does not occur with g++.

EXPECTED: Program compiles without error and prints "3".
OBSERVED: Program is rejected at compile time.

(The program is rejected when g++ or clang++ is invoked without
options.  That's not a bug.)

Using <math.h> rather than <cmath> doesn't change the symptom.

A similar program in C does not exhibit the problem.

I *think* the problem is in the "math.h" header, which should arrange
for M_PI and similar macros not to be defined in conforming mode.
It's also possible that a fix might involve updates to clang++.
I haven't fully investigated the twisty maze of macro definitions
and nested #includes in math.h.

(I acknowledge that writing your own definition of M_PI is not a
good idea, and that the definition in this program is particularly
poor style.)

I'm using 64-bit Cygwin on Windows 10, with all the latest updates.

See also
    https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/1810695
a report for a similar bug on Ubuntu (affecting both g++ and clang++).

Output illustrating the problem follows:

$ uname -a
CYGWIN_NT-10.0 eddie 2.11.2(0.329/5/3) 2018-11-08 14:34 x86_64 Cygwin

$ clang++ --version
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-unknown-windows-cygnus
Thread model: posix
InstalledDir: /usr/bin

$ cat M_PI_bug.cpp
#include <iostream>
#include <cmath>
int main() {
    const int M_PI = 22/7;
    std::cout << M_PI << '\n';
}

// This is not reasonable code.
// The issue is that a conforming C++ compiler must accept it.

// Expected output: 3

$ g++ -std=c++17 -pedantic-errors M_PI_bug.cpp && ./M_PI_BUG
3

$ clang++ -std=c++17 -pedantic-errors M_PI_bug.cpp && ./M_PI_BUG
M_PI_bug.cpp:4:15: error: expected unqualified-id
    const int M_PI = 22/7;
              ^
/usr/include/math.h:617:15: note: expanded from macro 'M_PI'
#define M_PI            3.14159265358979323846
                        ^
1 error generated.

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019