[Bug c++/91803] New: statement-expressions are not allowed in template arguments

bernd.edlinger at hotmail dot de gcc-bugzilla@gcc.gnu.org
Tue Sep 17 20:37:00 GMT 2019


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91803

            Bug ID: 91803
           Summary: statement-expressions are not allowed in template
                    arguments
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bernd.edlinger at hotmail dot de
  Target Milestone: ---

I wanted to use something like __typeof(ORIGINAL_REGNO(rtl)) in a template
argument, but it does not work, I wonder if that is a bug.

$ cat test.cc 
template <class X>
X identity(X x)
{ return x; }

#define IDENTITY(X) __extension__ \
({ __typeof(X) const x = (X); x; })

int
main()
{
  /* error: statement-expressions are not allowed
     outside functions nor in template-argument lists  */
  if (identity<__typeof(IDENTITY(0))>(1) != 1);
    __builtin_abort();

  /* but this works! */
  if (identity<__typeof(identity<__typeof(0)>(0))>(1) != 1)
    __builtin_abort();
}

$ g++ test.cc
test.cc: In function 'int main()':
test.cc:6:1: error: statement-expressions are not allowed outside functions nor
in template-argument lists
    6 | ({ __typeof(X) const x = (X); x; })
      | ^
test.cc:13:25: note: in expansion of macro 'IDENTITY'
   13 |   if (identity<__typeof(IDENTITY(0))>(1) != 1);
      |                         ^~~~~~~~
test.cc:13:40: error: no matching function for call to 'identity<<expression
error> >(int)'
   13 |   if (identity<__typeof(IDENTITY(0))>(1) != 1);
      |                                        ^
test.cc:2:3: note: candidate: 'template<class X> X identity(X)'
    2 | X identity(X x)
      |   ^~~~~~~~
test.cc:2:3: note:   template argument deduction/substitution failed:
test.cc:13:40: error: template argument 1 is invalid
   13 |   if (identity<__typeof(IDENTITY(0))>(1) != 1);
      |                                        ^


More information about the Gcc-bugs mailing list