Bug 82239 - Parentheses around constexpr template member break static_assert
Summary: Parentheses around constexpr template member break static_assert
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.2.0
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-09-18 16:23 UTC by Eelco Dolstra
Modified: 2020-10-20 14:35 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-10-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eelco Dolstra 2017-09-18 16:23:44 UTC
With GCC 6.4 or 7.2, the following example fails to compile in C++14 mode:

=== start ===
template<typename T>
struct C {
   static constexpr int x = 5;
   void f()
   {
      static_assert(0 < x, "x"); // ok
      static_assert(0 < (x), "x"); // fails
      static_assert(true || (0 < x), "x"); // ok
      static_assert(true || (0 < (x)), "x"); // fails
   }
};
=== end ===

$ g++ -c -std=c++14 foo.cc
foo.cc: In member function ‘void C<T>::f()’:
foo.cc:7:7: error: non-constant condition for static assertion
       static_assert(0 < (x), "x"); // fails
       ^~~~~~~~~~~~~
foo.cc:9:7: error: non-constant condition for static assertion
       static_assert(true || (0 < (x)), "x"); // fails
       ^~~~~~~~~~~~~

OTOH, -std=c++11 works fine. GCC 5.4 with -std=c++14 also compiles.

Also, removing "template<typename T>" or moving the definition of "x" out of the template makes it compile.

Tested on x86_64-linux (NixOS). Compiler info:

$ g++ -v
Using built-in specs.                                                                                                                                                                                                                                        
COLLECT_GCC=/nix/store/l9nq5bvqgjngqhlq7ks6cg0bdizn2l0v-gcc-7.2.0/bin/g++                                                                                                                                                                                    
COLLECT_LTO_WRAPPER=/nix/store/l9nq5bvqgjngqhlq7ks6cg0bdizn2l0v-gcc-7.2.0/libexec/gcc/x86_64-unknown-linux-gnu/7.2.0/lto-wrapper                                                                                                                             
Target: x86_64-unknown-linux-gnu                                                                                                                                                                                                                             
Configured with:                                                                                                                                                                                                                                             
Thread model: posix                                                                                                                                                                                                                                          
gcc version 7.2.0 (GCC)
Comment 1 Marek Polacek 2020-10-20 14:11:58 UTC
Fixed by r256550 but the test is useful.
Comment 2 GCC Commits 2020-10-20 14:35:10 UTC
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:953277ba3fa39a9285cf89f59932b0169e7f6b9d

commit r11-4126-g953277ba3fa39a9285cf89f59932b0169e7f6b9d
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Oct 20 10:15:41 2020 -0400

    c++: Add fixed test [PR82239]
    
    This test was fixed by r256550 but that commit was fixing another issue,
    and just happened to fix this too.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/82239
            * g++.dg/cpp0x/static_assert16.C: New test.
Comment 3 Marek Polacek 2020-10-20 14:35:51 UTC
Fixed.