|
Thanks Rajkumar,
Rajkumar R wrote: your example is quite enough to prove, just compile the code (don't build, i mean don't link), the code successfully compiles.
I think you mean the sample in my original question, right? If yes, why if compile successful implies f is instantised by g(int) declaration other than f(2) inside h()? How do you prove it?
regards,
George
|
|
|
|
|
George_George wrote: I think you mean the sample in my original question, right?
yes.
George_George wrote: why if compile successful implies f is instantised by g(int) declaration other than f(2) inside h()?
I would reformat this phrase,
if compile successful implies, at the point of function template instantiation 'void f<int> (T)' (i.e f(2) inside h()) being compiled g(a) is deduced to g(int), whose declaration is in the nearest global( or namespace, if called by scope resolution operator) scope.
actually f(2) in h() is used instantiate the template function f(T), where <T> is deduced to <int>
|
|
|
|
|
Thanks Rajkumar,
1.
I think you mean f(2) inside h will instantise template function f, right?
2.
When do you think g(int) will be bound to template function f? During the time of instantiation or during the time of compiler parsing definition of template function f?
(discuss in MSVC context and behavior is fine)
regards,
George
|
|
|
|
|
When a function template is first called for each type (f(2) in h), the compiler creates a version of the templated function specialized for the type 'void f<int> (T)', which is called Function .template instantiation [^].
1) yes, see above.
2)
George_George wrote: During the time of instantiation or during the time of compiler parsing definition of template function f?
see above, without parsing the template function f, how it can create a version of that function, called instantiation.
As a side note function template instantiation requires the definition of the template function to be visible ( f<T> ) at the point where it is refered in the code (i.e when calling template function), may be you confused this with the definition of g() with is non-template function. declaration of g() is sufficient for calling g() to be compiled, which applicable to non-template context also.
And question, declaration of g() will instantise f<int> (T) is true. since g is non inline non template function.
try commenting the declaration of g(); you will get compilation error, identifier g, not found when instantiating funtion void f<int>(T), otherwise not; this is the proof.
|
|
|
|
|
Thanks Rajkumar,
1.
I studied your recommended link and it is great. I found some knowledge which I do not have before. Previously, I think only use of the template function will trigger its instantiation, but now I have found at least two ways to explicitly instantise template function, besides the "use of the template function methods". The two methods are,
template<class T> void f(T) { }
template void f<int> (int);
2.
Rajkumar R wrote: And question, declaration of g() will instantise f<int> (T) is true. since g is non inline non template function.
try commenting the declaration of g(); you will get compilation error, identifier g, not found when instantiating funtion void f<int>(T), otherwise not; this is the proof.
I have tried the following code will not cause any compile errors, using Visual Studio 2008. I doubt whether the declaration of g will trigger the instantiation of template function f?
template <class T> void f(T a) {g (a);}
void g (int a);
regards,
George
|
|
|
|
|
Ahh, Now i read your orginal post and understand the question, i actually missed the following
George_George wrote:
the point of instantiation for f<int>() is just before h(), so the g() called in f() is the global g (int) rather than the local g (double).
--------------------
BTW: from above statement from Bjarne, I think he means declaration of g(int) before h() will instiatise f to f <int>.
I understand from the bjarne's statement is that the point of instantiation is at the template function declaration template <typename T> void f (T) which is before the function h() which is instantised by f<int>() call in h(), since the template deduce functions at the global or namespace scope global g() is taken rather than local g(double).
g(int) won't trigger instantiation of f<int>(), template function instantiation occurs when it is called by special type template arguments. All that reply before was keeping in mind f<int> () is instantiated with g(int) triggered by the call f(int) in h.
|
|
|
|
|
Thanks Rajkumar!
I found after discussion with you these days, most of my confusions are solved. Cool!!
But still one confusion can not be solved, I show my confusion in below code.
I do not know why there is compile error below (compile error posted), and if I remove statement "cout << d.get_i() << endl; // output 200", there will not be any compile errors?
My confusion is, I think whether or not we call get_i, Derived<int> is instantised, right? Instantised means having the same code, why whether we call a member function or not will impact compile result? Is there some new rules we do not cover theses days about instantiation of template class?
Compile errors,
1>------ Build started: Project: test_template4, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>d:\visual studio 2008\projects\test_template4\test_template4\main.cpp(15) : error C2955: 'Base' : use of class template requires template argument list
1> d:\visual studio 2008\projects\test_template4\test_template4\main.cpp(5) : see declaration of 'Base'
1> d:\visual studio 2008\projects\test_template4\test_template4\main.cpp(21) : see reference to class template instantiation 'Derived<T>' being compiled
1>d:\visual studio 2008\projects\test_template4\test_template4\main.cpp(15) : error C2955: 'Base' : use of class template requires template argument list
1> d:\visual studio 2008\projects\test_template4\test_template4\main.cpp(5) : see declaration of 'Base'
1> d:\visual studio 2008\projects\test_template4\test_template4\main.cpp(26) : see reference to class template instantiation 'Derived<T>' being compiled
1> with
1> [
1> T=int
1> ]
#include <iostream>
using namespace std;
template <typename T> struct Base {
public:
Base (int _i): i (_i)
{
}
int i;
};
template <typename T> struct Derived : public Base {
public:
Derived (int _i) : Base<T> (_i)
{
}
int get_i() { return i; }
};
int main()
{
Derived<int> d (200);
cout << d.get_i() << endl;
return 0;
}
regards,
George
|
|
|
|
|
George_George wrote: Is there some new rules we do not cover theses days about instantiation of template class?
I think yes, in this thread we discussed about function template, please refer Class Template Instantiation
[^] and note the following from the link
"No code is generated for a templated class or function until it is instantiated.
Moreover, member functions are instantiated only if they are called"
|
|
|
|
|
Cool, Rajkumar!
Question answered.
regards,
George
|
|
|
|
|
Hello everyone,
For the MSDN template sample,
http://msdn2.microsoft.com/en-us/library/w98s4hs8.aspx
My questions are,
1. does void [Code]f(char) { printf_s("f(char)\n");}[/Code] or [Code]g('c');[/Code] instantise function g to parameter type char?
2. "This can cause overloads to be declared after the template (but before the template is instantiated) to be seen." What does this mean?
thanks in advance,
George
|
|
|
|
|
When template is declared, the compiler takes into account also the char overload of the function N::f and it shouldn't (because it is declared after template declaration itself: please note I wasn't aware about this rule).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Thanks CPallini,
What is your answer and comments to my question (1) in the original reply? And why you make such answer?
regards,
George
|
|
|
|
|
George_George wrote: What is your answer and comments to my question (1) in the original reply?
Actually the answer was to both your points.
George_George wrote: And why you make such answer?
It's common sense applied to my own understanding of the MSDN page.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Sorry CPallini,
I do not fully understand your reply. Do you mean void
f(char) { printf_s("f(char)\n");
or
g('c');
instantise function g to parameter type char? Or both?
regards,
George
|
|
|
|
|
When compiler finds g('c') , if it knows there is a f(char) then it should use it. The problem here is that specification states that compiler must not consider f(char) because it is declared after the template itself, hence the right behaviour is calling f(int) .
But, as you know (thanks to Iain) this is going on my arrogant assumptions...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Thanks CPallini,
Question answered. I notice you know almost everything about C++. I am learning C#, and I am going to test your C# knowledge in days.
regards,
George
|
|
|
|
|
You're too fast for me. My knowledge about C# is really poor.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Thanks in advance, CPallini!
I hope I will grasp basic skills and begin to ask questions in 2 monthes. I will read through the C# Programming Language, it is good it is not written by Bjarne again, which is hard to read.
regards,
George
|
|
|
|
|
George_George wrote: the C# Programming Language
This one?
kind regards.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Yes, good book.
regards,
George
|
|
|
|
|
hi friends
i need a very simple compiler that created by visual c++ for my undergraduate compiler course project and i also need a source of that compiler!!!! please help me!
tanx alot
|
|
|
|
|
faradgi wrote: please help me!
Hi faradgi,
What kind of help are you expecting? Are you expecting the people over here to write a compiler for you? Sorry, that won't happen. You must do your own assignment and you may ask questions here, if you run into trouble.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
I suggest you to read the book of Wirth, Compiler Construction.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
|
Hi all,
i m new in VC++2005, now i m working on audio-video related software.
i have a problem in reading and writing of video file.
i want to read video file of any format and write selected part of it in any other file.
but i dont know how can it possible.
so please anyone can help me.
thanks in advance
|
|
|
|
|