Click here to Skip to main content
15,908,175 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: anti-standard code can compile -- about template class Pin
George_George7-Mar-08 0:19
George_George7-Mar-08 0:19 
GeneralRe: anti-standard code can compile -- about template class Pin
BadKarma7-Mar-08 1:11
BadKarma7-Mar-08 1:11 
GeneralRe: anti-standard code can compile -- about template class Pin
George_George7-Mar-08 1:13
George_George7-Mar-08 1:13 
GeneralRe: anti-standard code can compile -- about template class Pin
BadKarma7-Mar-08 1:35
BadKarma7-Mar-08 1:35 
GeneralRe: anti-standard code can compile -- about template class Pin
George_George7-Mar-08 1:39
George_George7-Mar-08 1:39 
GeneralRe: anti-standard code can compile -- about template class Pin
Mike Dimmick7-Mar-08 14:30
Mike Dimmick7-Mar-08 14:30 
GeneralRe: anti-standard code can compile -- about template class Pin
George_George7-Mar-08 20:25
George_George7-Mar-08 20:25 
GeneralRe: anti-standard code can compile -- about template class Pin
Mike Dimmick8-Mar-08 1:34
Mike Dimmick8-Mar-08 1:34 
A template is instantiated for a given set of parameters when it is first used, so it is g('c') which instantiates g<char>. The template argument is deduced from the type of the argument to the function call.

The problem here is that the standard calls for anything not dependent on the template parameters (the stuff between <> ) to be bound at the point that the template is defined, not the point that it is instantiated. Only those references that depend upon the template parameters are supposed to be deferred until the point that the template is instantiated.

In the example given at that link:
namespace N {
   void f(int) { printf("f(int)\n");}
}
 
template <class T> void g(T) {
   N::f('a');   // calls f(char) should call f(int)
}
 
namespace N {
   void f(char) { printf_s("f(char)\n");}
}
 
int main() {
   g('c');
}
The standard says that when the definition of g(T) is encountered, anything not explicitly dependent on the argument T should be bound. At this point we have encountered the declaration of N::f(int) but not yet the declaration of N::f(char). Because char is implicitly convertable to int, a standard-compliant compiler would bind to N::f(int). If the declaration of N::f(int) wasn't there, an error would occur.

Visual C++ at present does not do this first step of binding and defers all name resolution to the point of instantiation. This is when the call to g is encountered. At this point the compiler has seen both overloads of N::f so it binds to N::f(char) as that is the best match.

Say instead the declaration of g were:
template <class T> void g(T t) {
   N::f(t);
}
Now the call to N::f is dependent on the type of the parameter t which is the template argument T. Here, the compiler cannot bind the call to N::f when it encounters the template definition because it does not know what T is. It has to defer it until the template is instantiated. When the call to g('c') is encountered, it knows that the type parameter T is char, so it generates a call to N::f(char). This is true of both Visual C++ and a standards-compliant compiler.

In your original example, the use of the variable i is not explicitly dependent on the template argument T so it is assumed not to be dependent. A standard compiler tries to bind to a global i and generates an error if one is not found. Visual C++ will wait until the template is instantiated and bind to the base class's i member as that is the closest i in scope at the point of instantiation. To make the standards compiler wait until the point of instantiation, you have to make the expression depend on the base class: if i is expected to be an instance member, use this->i, otherwise if it's a static member use Base<T>::i.


DoEvents: Generating unexpected recursion since 1991

GeneralRe: anti-standard code can compile -- about template class Pin
George_George8-Mar-08 22:15
George_George8-Mar-08 22:15 
QuestionHow Can Attach Bar Visulizer? Pin
Le@rner6-Mar-08 20:26
Le@rner6-Mar-08 20:26 
QuestionHow to Get Task List in Windows Pin
somasundarambe6-Mar-08 19:12
somasundarambe6-Mar-08 19:12 
AnswerRe: How to Get Task List in Windows Pin
Rajkumar R6-Mar-08 19:36
Rajkumar R6-Mar-08 19:36 
GeneralRe: How to Get Task List in Windows Pin
nitin36-Mar-08 20:14
nitin36-Mar-08 20:14 
GeneralRe: How to Get Task List in Windows Pin
Rajkumar R6-Mar-08 21:24
Rajkumar R6-Mar-08 21:24 
GeneralRe: How to Get Task List in Windows Pin
somasundarambe6-Mar-08 21:45
somasundarambe6-Mar-08 21:45 
Generalqualified name and unqualified name Pin
George_George6-Mar-08 18:29
George_George6-Mar-08 18:29 
AnswerRe: qualified name and unqualified name Pin
Maxwell Chen6-Mar-08 18:36
Maxwell Chen6-Mar-08 18:36 
GeneralRe: qualified name and unqualified name Pin
George_George6-Mar-08 18:42
George_George6-Mar-08 18:42 
GeneralRe: qualified name and unqualified name Pin
George_George6-Mar-08 18:46
George_George6-Mar-08 18:46 
GeneralRe: qualified name and unqualified name Pin
Maxwell Chen6-Mar-08 18:56
Maxwell Chen6-Mar-08 18:56 
GeneralRe: qualified name and unqualified name Pin
George_George6-Mar-08 19:00
George_George6-Mar-08 19:00 
GeneralRe: qualified name and unqualified name Pin
Maxwell Chen6-Mar-08 19:14
Maxwell Chen6-Mar-08 19:14 
GeneralRe: qualified name and unqualified name Pin
George_George6-Mar-08 21:21
George_George6-Mar-08 21:21 
GeneralRe: qualified name and unqualified name Pin
George_George6-Mar-08 19:15
George_George6-Mar-08 19:15 
GeneralRe: qualified name and unqualified name Pin
Maxwell Chen6-Mar-08 20:18
Maxwell Chen6-Mar-08 20:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.