4

Here's what the standard says about non-dependent names in a template definition:

Non-dependent names used in a template definition are found using the usual name lookup and bound at the point they are used.

[Example 1:

void g(double); 
void h();

template<class T> class Z { 
public:   
  void f() {
    g(1);           // calls g(double)
    h++;            // ill-formed: cannot increment function; this could be diagnosed
                    // either here or at the point of instantiation   
  } 
};

void g(int);        // not in scope at the point of the template definition, not considered for the call g(1) 

— end example]


I'm confused by the comment on h++; that says "ill-formed: ... this could be diagnosed either here or at the point of instantiation". What if an implementation chooses the latter, but there isn't an instantiation of the template? Where would this be diagnosed?

Does that mean this is actually ill-formed, no diagnostic required instead?

cigien
  • 57,834
  • 11
  • 73
  • 112

1 Answers1

3

Ill-formed NDR is a specific case of ill-formed. In the example, the definition of class template Z clearly is ill-formed; whether or not a diagnostic is required depends on whether or not an instantiation of Z occurs anywhere in the translation unit, which the example appears agnostic about (since it admits that there may be a point of instantiation). If there is indeed an instantiation, then the implementation has freedom to issue the diagnostic either at the point of definition or at the point of instantiation. See [temp.res.general]/8.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • Ok, so [this](https://eel.is/c++draft/temp.res#general-8.4) bullet covers the example above, correct? – cigien Oct 25 '20 at 00:40
  • @cigien I believe it is covered by both 8.1 and 8.4 but I'm not an expert in the subtleties behind those two points. – Brian Bi Oct 25 '20 at 00:43
  • That's true, 8.1 seems to cover that too. I think this explains it, but I'll hold off on accepting the answer for a bit. Thanks :) – cigien Oct 25 '20 at 00:45
  • Wait, IFNDR is supposed to be explicitly stated right? Doesn't IF mean a diagnostic *is* required? – cigien Oct 25 '20 at 02:55
  • @cigien In the normative portion of the text, yes. There is an implicit requirement of a diagnostic whenever the text says that something is ill-formed. Thus it can only be ill-formed NDR if this the NDR part is explicitly stated. Examples are not normative, and I suspect whoever wrote this example simply didn't anticipate the possibility of confusion. – Brian Bi Oct 25 '20 at 02:58
  • ok, so the comment in the example is wrong? so you're saying the example is not ill-formed? Your answer says otherwise. – cigien Oct 25 '20 at 03:08
  • @cigien The example is ill-formed. It is either ill-formed with a diagnostic required or ill-formed with no diagnostic required (depending on whether the template is instantiated somewhere else in the TU), both of which are ill-formed. However, when the Standard contains some sentence like "if X, then the program is ill-formed" then we must remember to read it as excluding the NDR case. – Brian Bi Oct 25 '20 at 03:12
  • But I think this rule of interpretation doesn't necessarily apply to the comments in examples. – Brian Bi Oct 25 '20 at 03:17
  • Ah, looks like [13.8.1.10](https://eel.is/c++draft/temp.res#general-10) does have normative text :) and a better comment :) The last part of the comment above is just wrong. – cigien Oct 25 '20 at 03:56
  • @cigien That paragraph is about something different. In the example, the name is in scope, it's just that the operation that's done on the entity it denotes is invalid. – Brian Bi Oct 25 '20 at 03:59
  • Oh, of course. I guess it's easy to check if a name is in scope, but once in scope it may be hard in general to know if it can be used some way. Thanks, that helped. So no normative text either way here means it's IFNDR. That's reasonable. – cigien Oct 25 '20 at 04:05
  • Strictly speaking, [temp.res] doesn't apply to `f`, because it is not a template. See [CWG1253](http://wg21/cwg1253). – Language Lawyer Oct 25 '20 at 14:36
  • @LanguageLawyer The text referred to in CWG1253 has changed considerably. Does it make the defect moot? I can't tell. – cigien Oct 25 '20 at 19:45