Ref: Function template default arguments: C++ defect report 226
(http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#226)

GCC 4.5.0 (tr...@155642) compiles the following code despite the
result of sizeof being undefined.  The use of sizeof(incomplete-type)
as the value of a default template argument results in the argument
being given the value zero rather than causing a compile error.

I was originally trying an SFINAE technique with a parameter pack and
as such tried applications of sizeof to variations of templates (as
included below).  However the bug is exposed by the following reduced
case:

  struct undef;

  template <typename U, int N = sizeof(U)>
  int f()
  {
     return N;
  }

  int main()
  {
     // compiles and returns zero
     return f<undef>();

     // the above should fail to compile in the same way as this
     // explicitly specified version does.
     //   return f<undef, sizeof(undef)>();
  }


A more elaborate source yielding the same result is:

  struct undef;
  template <typename> struct undef_single;
  template <typename,typename,typename> struct undef_three;
  template <typename...> struct undef_pack;

  template <typename U, int N = sizeof(U)>
  int f()
  {
     return N;
  }

  template <typename U, int N = sizeof(undef_single<U>)>
  int f1()
  {
     return N;
  }

  template <typename U1, typename U2, typename U3, int N =
sizeof(undef_three<U1,U2,U3>)>
  int f3()
  {
     return N;
  }

  template <typename... U, int N = sizeof(undef_pack<U...>)>
  int fp()
  {
     return N;
  }

  int main()
  {
  // all alternatives compile and return zero
  //
  // return fp<int,float,short>();
  // return f3<int,float,short>();
  // return f1<int>();
     return f<undef>();


  // yields the result I would expect from the above (i.e. fail to
  // compile)
  //
  // return fp<int,float,short, sizeof(undef_pack<int,float,short>)>();
  // return f3<int,float,short, sizeof(undef_three<int,float,short>)>();
  // return f1<int,float,short, sizeof(undef_single<int>)>();
  // return f<undef, sizeof(undef)>();
  }


-- 
           Summary: Function template default arguments: Invalid expressions
                    are allowed
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: adam at jessamine dot co dot uk
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42623

Reply via email to