Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are deleted constructors "accessible"?

A deleted answer on this question about a deleted move constructor quotes cppreference.com as saying that the is_move_constructible trait should succeed as long as a move constructor is "accessible", even if it's not "usable".

The standard in fact requires that move-construction of the argument type be well-formed, so the answer was not quite right.

Now, the standard repeatedly uses the term "accessible" in relation to constructors referring to actual constructibility. For example:

[C++11 8.5/6]: To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is an array type, each element is default-initialized;
  • otherwise, no initialization is performed.

If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.

However, I can't find anywhere in the standard that states categorically whether a deleted, explicitly-defined constructor is "accessible" or not.

A different [non-normative] quote seems to suggest that deleted-ness and accessibility are orthogonal:

[C++11: 12.2/1]: [..] [ Note: even if there is no call to the destructor or copy/move constructor, all the semantic restrictions, such as accessibility (Clause 11) and whether the function is deleted (8.4.3), shall be satisfied. [..]

  • Have I missed a passage?
  • If not, should the cppreference.com page be corrected? Can you suggest a better wording?
  • Should the standard be clearer about this either way?
like image 815
Lightness Races in Orbit Avatar asked Dec 28 '13 15:12

Lightness Races in Orbit


1 Answers

I don't want to address what the cppreference website says, but as far as the standard is concerned, constructibility is not defined in terms of "accessible constructors". Rather, the primary definition is that of is_constructible, which is (C++11, 20.9.4.3/6):

is_constructible<T, Args...>

shall be satisfied if and only if the following variable definition would be well-formed for some invented variable t:

T t(create<Args>()...);

Access checking is performed as if in a context unrelated to T and any of the Args. Only the validity of the immediate context of the variable initialization is considered.

So the well-formedness of the hypothetical expression on the last line of code is the defining charac­ter­istic for the constructibility traits. And that works hand-in-hand with the clause that says that using a de­leted function leads to an ill-formed program.

like image 81
Kerrek SB Avatar answered Oct 07 '22 08:10

Kerrek SB