Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385114

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. [..]

Upvotes: 16

Views: 577

Answers (2)

Kerrek SB
Kerrek SB

Reputation: 476990

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.

Upvotes: 12

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385114

From the second quote in the question, I'd say that accessibility is not affected by deletedness, and that the first quote actually doesn't at all cover the case that such a constructor may be deleted.

This scenario is instead covered by a sort of "catch-all" requirement in the definition of delete:

[C++11: 8.4.3/2]: A program that refers to a deleted function implicitly or explicitly, other than to declare it, is ill-formed. [ Note: This includes calling the function implicitly or explicitly and forming a pointer or pointer-to-member to the function. It applies even for references in expressions that are not potentially-evaluated. If a function is overloaded, it is referenced only if the function is selected by overload resolution. —end note ]

So, cppreference.com could probably do with a note that there is a further criterion that applies to the is_move_constructible trait, than simply whether the move constructor is accessible. And there's a further problem here, which is that MoveConstructible can be satisfied by CopyConstructible too, so even the move constructor on its own is not strictly necessary.

This all raises another interesting point, though, that any possible implementation of is_move_constructible must surely "refer" to the deleted move constructor, which renders the program ill-formed as stated in the quote above. Still, I suppose with SFINAE tricks an implementation can avoid actually becoming ill-formed.


"A type with no move ctor at all but with a copy-ctor is move-constructible (constructible from an rvalue)." — DyP

Upvotes: 9

Related Questions