Title
Array of abstract instance of class template
Status
cd5
Section
9.3.4.5 [dcl.array]
Submitter
Richard Smith

Created on 2013-03-14.00:00:00 last changed 37 months ago

Messages

Date: 2018-06-15.00:00:00

[Adopted at the June, 2018 meeting as part of papeer P0929R2.]

According to 9.3.4.5 [dcl.array] paragraph 1, an array declarator whose element type is an abstract class is ill-formed. However, if the element type is a class template specialization, it may not be known that the class is abstract; because forming an array of an incomplete type is permitted (6.8 [basic.types] paragraphs 5-6), the class template is not required to be instantiated in order to use it as an element type. The expected handling if the class template is later instantiated is unclear; should the compiler issue an error about the earlier array array type at the point at which the class template is instantiated?

This also affects overload resolution:

  template<typename> struct Abstract {
    virtual void f() = 0;
    typedef int type;
  };
  template<typename T> char &abstract_test(T[1]);      // #1
  template<typename T> char (&abstract_test(...))[2];  // #2
  // Abstract<int>::type n;
  static_assert(sizeof(abstract_test<Abstract<int>>(nullptr)) == 2, "");

Overload resolution will select #1 and fail the assertion; if the commented line is uncommented, there is implementation variance, but presumably #2 should be selected and satisfy the assertion.

These effects of completing the type are troublesome. Would it be better to allow array types of abstract element type and simply prohibit creation of objects of such arrays?

(See also issue 1646.)

History
Date User Action Args
2021-02-17 00:00:00adminsetstatus: drafting -> cd5
2013-05-03 00:00:00adminsetstatus: open -> drafting
2013-03-14 00:00:00admincreate