Title
Non-dependent static_assert-declarations
Status
nad
Section
13.8 [temp.res]
Submitter
Mike Miller

Created on 2012-03-23.00:00:00 last changed 139 months ago

Messages

Date: 2012-10-15.00:00:00

Rationale (October, 2012):

Although this usage is supported by some implementations and used in some libraries, CWG felt that =delete is the appropriate mechanism for making a function template or member function of a class template unavailable for specialization.

Date: 2022-11-20.07:54:16

Recently a customer sent us code of the form,

  template<typename T> void f();
  template<> void f<int>() { }
  template<typename T> void f() {
    static_assert(false, "f() instantiated with non-int type.");
  }

The intent, obviously, was to do compile-time diagnosis of specializations of the template that were not supported, and code of this form is supported by at least some implementations. However, the current wording of 13.8 [temp.res] paragraph 8, appears to invalidate this approach:

If no valid specialization can be generated for a template, and that template is not instantiated, the template is ill-formed, no diagnostic required.

In this example, the static_assert will fail for every generated specialization of f(), so an implementation can issue the error, regardless of whether f() is ever instantiated with a non-int type or not.

A relatively straightforward but somewhat ugly workaround is to define a template like

  template<typename> struct always_false {
    static const bool val = false;
  };

and replace the use of false in the static_assert with always_false<T>::val, making the static_assert dependent.

Considering the fact that a non-dependent static_assert-declaration in a template is otherwise pretty useless, however, it might be worth considering whether to support this usage somehow, especially in light of the fact that it is supported by some implementations, perhaps by treating static_assert-declarations as always dependent, even if the condition is not otherwise dependent.

History
Date User Action Args
2012-11-03 00:00:00adminsetmessages: + msg4181
2012-11-03 00:00:00adminsetstatus: open -> nad
2012-03-23 00:00:00admincreate