The book you are looking at seems to reflect (mainly) that the author is watching how the compilers work, and not according to the requirement (standards) of the standard. The standard doesnβt say much about giving the compiler additional leniency about incorrect code inside the template, simply because it has not been created.
At the same time, the book is true that there are some things that the compiler really canβt do much to check until it is created. For example, you can use the dependent name as the name of the function (or call it as a function, one way or another) if it is a functor, and that will be good too) If you instantiate this template above the class where it is a function, fine and good. If you create an instance of a class where it is really int , an attempt to call it will certainly fail. Until you create an instance, the compiler cannot determine what exactly.
This is a big part of what concepts were really intended to be added to C ++. You can directly specify (for example) that the pattern X will call T::y as a function. Then the compiler could compare the contents of the template with the declarations in the concept and determine whether the body of the template matches the declaration in the concept or not. In another direction, the compiler needed to compare a class (or something else) with a concept to determine if an instance of this template would work. If it does not work, it can report the error directly as a violation of the corresponding concept (since now it is trying to create an instance of the template, and you often get some strange error message that poorly indicates the real problem, if at all).
Jerry Coffin
source share