32

Consider this code:

struct A; // incomplete type

template<class T>
struct D { T d; };

template <class T>
struct B { int * p = nullptr; };

int main() {
    B<D<A>> u, v;
    u = v;  // doesn't compile; complain that D<A>::d has incomplete type
    u.operator=(v); // compiles
}

Demo. Since u.operator=(v) compiles but u = v; doesn't, somewhere during the overload resolution for the latter expression the compiler must have implicitly instantiated D<A> - but I don't see why that instantiation is required.

To make things more interesting, this code compiles:

struct A; // incomplete type

template<class T>
struct D; // undefined

template <class T>
struct B { int * p = nullptr; };

int main() {
    B<D<A>> u, v;
    u = v;
    u.operator=(v);
}

Demo.

What's going on here? Why does u = v; cause the implicit instantiation of D<A> - a type that's nowhere used in the body of B's definition - in the first case but not the second?

7
  • On Visual Studio 2013 it compiles, but gives runtime error. Run-Time Check Failure #3 - The variable 'v' is being used without being initialized. on u = v; on both tests.
    – KugBuBu
    Commented Sep 21, 2014 at 11:13
  • 1
    @KugBuBu That's an unrelated issue. I added an initializer for p in the code just in case; nothing changed. (And VS's template engine is notoriously non-standard, so...)
    – T.C.
    Commented Sep 21, 2014 at 11:46
  • 1
    Have you considered whether the lookup for the assignment-operator requires instantiating related types due to ADL? Classes that appear as template arguments are related as per [basic.lookup.argdep]/2
    – dyp
    Commented Sep 21, 2014 at 12:46
  • 1
    (It doesn't make much sense for assignment, as it can only be implemented as a member function, though. I'm not sure however how the implementers interpret the algorithm in [over.match.oper]/3; maybe they always perform this lookup, even if there can be no non-member results.)
    – dyp
    Commented Sep 21, 2014 at 12:53

2 Answers 2

20

The entire point of the matter is ADL kicking in:

N3797 - [basic.lookup.argdep]

When the postfix-expression in a function call (5.2.2) is an unqualified-id, other namespaces not considered during the usual unqualified lookup (3.4.1) may be searched, and in those namespaces, namespace-scope friend function or function template declarations (11.3) not otherwise visible may be found.

following:

For each argument type T in the function call, there is a set of zero or more associated namespaces and a set of zero or more associated classes to be considered. [...] The sets of namespaces and classes are determined in the following way:

  • If T is a class type [..] its associated classes are: ... furthemore if T is a class template specialization its associated namespaces and classes also include: the namespaces and classes associated with the types of the template arguments provided for template type parameters

D<A> is an associated class and therefore in the list waiting for its turn.

Now for the interesting part [temp.inst]/1

Unless a class template specialization has been explicitly instantiated (14.7.2) or explicitly specialized (14.7.3), the class template specialization is implicitly instantiated [...] when the completeness of the class type affects the semantics of the program

One could think that the completeness of the type D<A> doesn't affect at all the semantic of that program, however [basic.lookup.argdep]/4 says:

When considering an associated namespace, the lookup is the same as the lookup performed when the associated namespace is used as a qualifier (3.4.3.2) except that:

[...] Any namespace-scope friend functions or friend function templates declared in associated classes are visible within their respective namespaces even if they are not visible during an ordinary lookup (11.3)

i.e. the completeness of the class type actually affects friends declarations -> the completeness of the class type therefore affects the semantics of the program. That is also the reason why your second sample works.

TL;DR D<A> is instantiated.

The last interesting point regards why ADL starts in the first place for

u = v; // Triggers ADL
u.operator=(v); // Doesn't trigger ADL

§13.3.1.2/2 dictates that there can be no non-member operator= (other than the built-in ones). Join this to [over.match.oper]/2:

The set of non-member candidates is the result of the unqualified lookup of operator@ in the context of the expression according to the usual rules for name lookup in unqualified function calls (3.4.2) except that all member functions are ignored.

and the logical conclusion is: there's no point in performing the ADL lookup if there's no non-member form in table 11. However [temp.inst]p7 relaxes this:

If the overload resolution process can determine the correct function to call without instantiating a class template definition, it is unspecified whether that instantiation actually takes place.

and that's the reason why clang triggered the entire ADL -> implicit instantiation process in the first place.

Starting from r218330 (at the time of writing this, it has been committed a few minutes ago) this behavior was changed not to perform ADL for operator= at all.


References

Thanks to Richard Smith and David Blaikie for helping me figuring this out.

7
  • Looks like there are two separate questions - 1) is the compiler required/allowed to instantiate D<A> for operator=? (I think we all agree that ADL requires instantiation for operators that can be non-members.) 2) In cases where D<A> is required to be instantiated (e.g., binary operator-) why does the second example compile at all? Implicitly instantiating a template that's declared but not defined should render the program ill-formed ([temp.inst]/p8).
    – T.C.
    Commented Sep 23, 2014 at 23:42
  • Clang definitely was for 1) before r218330. As for 2) its completeness doesn't affect program's semantic
    – Marco A.
    Commented Sep 24, 2014 at 5:38
  • Why not? Either proper ADL (in cases where the operator can be a non-member, at least) requires instantiation of D<A> or it doesn't. You can't have it both ways. (In the example linked in my comment, B::operator - isn't the best theoretically possible match for the expression u - v either, so [temp.inst]/p7 can't apply.)
    – T.C.
    Commented Sep 24, 2014 at 6:46
  • it does for that definition, but the point here is whether ADL should be performed for this code or not.
    – Marco A.
    Commented Sep 24, 2014 at 6:56
  • 2
    Aha, so I was on the right track. Thanks for being more persistent than me and checking this with the clang devs.
    – dyp
    Commented Sep 24, 2014 at 13:34
-2

Well, I think in Visual Studio 2013 code should look like(without = nullptr):

  struct A; // incomplete type

  template<class T>
  struct D { T d; };

  template <class T>
  struct B { int * p; };

  int void_main() {
    B<D<A>> u, v;
    u = v;          //  compiles
    u.operator=(v); // compiles
    return 0;
    }

In this case it should compile well just because incomplete types can be used for specific template class specialization usage.

As for the run-time error - the variable v used without initialization - it's correct - struct B does not have any constructor => B::p is not initialized and could contain garbage.

Not the answer you're looking for? Browse other questions tagged or ask your own question.