|
Hi CPallini,
Why statement extern g (double); will not instantise a double version f, i.e. f (double)?
regards,
George
|
|
|
|
|
And why should it do? The template function is f , not g .
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 CPallini,
Bjarne's point, void g(int) statement will instantise
template <class T> void f (T a) { g(a); }
by deducing the type of T int, since in g(a), type of a is T and we are declaring g(int).
My question is why extern g(double) does not have the same effect to deduct parameter T to double?
regards,
George
|
|
|
|
|
OK, things are more clear, at least to me, changing the sample into
template <class t=""> void f (T a) { g(a); }
void g(int x) { return;}
void h()
{
extern void g (double);
f (2.5);
}
</class>
I made a test (defining void g (double) ) into another source file. The result was that only int specialization of f was built by the compiler, confirming Bjarne point.
Now, while it is clear that g(double) is exposed by correnponding source file, maybe that its declaration make it scoped only into h() i.e. local.
I have to admit the point it is not so clear.
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 CPallini,
Thanks for your reply. Do you have any effective and efficient ways to debug template function to find out the type of template parameters?
regards,
George
|
|
|
|
|
George_George wrote: extern g (double);
extern is a sort of forward declaration... it doesn't define a g(double) function at all.
and if anywhere in your code, such a function exist, the only function to get called will be g(int) (because it's the only one which actually exist...)
|
|
|
|
|
Hi toxcct,
Sorry I do not agree with you. Please look at Rajkumar's reply which proves what matters is whether declaration is local or global, what not matters is whether using extern or whether a definition or delaration. I think both definition and declaration can make an instantiation.
Any comments? Please feel free to correct me if I am wrong.
regards,
George
|
|
|
|
|
George_George wrote: f (2);
Are you missing here something like f<double> (2); or f(2.0); ?
George_George wrote: Does it before g (double) is not global function or namespace scope enclosing its use? If yes, I do not know why g (double) is not a global function, since it is declared as extern and some other compile unit should expose it?
yes, the definition of the g(double) must be in global scope of some other compilation unit to specify the linkage specifier extern. but its declaration is not in global scope or namespace scope at the point of function template instantiation for 'void f<double>(T)' being compiled.
In your sample declaration is in function scope of h();
try this,
template <class T>
void f (T a)
{ g(a);
}
void g(int);
extern g (double);
void h()
{
f <double>(2);
}
|
|
|
|
|
Cool, Rajkumar!
1.
I think the original code does not work is because declaration of using g is local? Confirms to Bjarne'e comments -- "global or namespace scope enclosing its use, just before the declaration that contains that use"? Important points are global and declaration. Right?
--------------------
Each use of a template for a given set of template arguments defines a point of instantiation.
That point is the nearest global or namespace scope enclosing its use,
just before the declaration that contains that use.
--------------------
2.
You like explicitly assign parameter for template function, like f<double>(2), other than f(2). Is it for the good of reducing ambiguity? Any other benefits?
regards,
George
|
|
|
|
|
George_George wrote: Important points are global and declaration. Right?
yes.
George_George wrote: You like explicitly assign parameter for template function, like f <double>(2), other than f(2).
in your argument you want to deduce double but you specified f(2) not f(2.0), if you have this tendency always go for explicit deducing template function like f<double>(2);
|
|
|
|
|
|
George_George wrote: I think both declaration and definition can instantise the template function, right?
actually consider declaration, definition implicitly has declaration. if it is inline function, definition should be in the scope at the point of intantiation.
|
|
|
|
|
Thanks Rajkumar,
Your reply is clear. Definition (which is also declaration implicitly) and declaration both will trigger instantiation.
regards,
George
|
|
|
|
|
Hi Rajkumar,
I have a further question. I have tried similar code as yours. But can not compile in Visual Studio 2008. Any ideas? f<int> (100) works. Why?
BTW: a fix of
1>d:\visual studio 2008\projects\test_template3\test_template3\main.cpp(16) : error C2783: 'void f(int)' : could not deduce template argument for 'T'
1> d:\visual studio 2008\projects\test_template3\test_template3\main.cpp(5) : see declaration of 'f'
#include <iostream>
using namespace std;
template <class T> void f(int a) {g (a);}
void g(int a)
{
cout << a << endl;
}
extern void g(double);
int main()
{
f (100);
return 0;
}
regards,
George
|
|
|
|
|
George_George wrote: I have tried similar code as yours
it is not at all similar.
in my example it is
template <class T> void f(T a) {g (a);}
template <class T> void f(int a) {g (a);} in this Template argument T is not addressed, in this case you want call like,
f<char> (100); // i used char just to deduce T.
similarly
template <class T1, class T2> void f(T2 a) {g (a);} here also template argument T1 is not deduced. here also you can use f<char> (100);
if you want template specialization, use like this,
void g(int a)
{ cout << a << endl;
}
extern void g(double);
template <class T>
void f(T a)
{
g (a);
}
<code>template <>
void f(int a)
{
g (a);
}
or
template <>
void f <int>(int a)
{
g (a);
}</code>
int main()
{
f (100);
return 0;
}
modified on Thursday, March 6, 2008 7:20 AM
|
|
|
|
|
Thanks Rajkumar,
I have tried if we remove the following two functions, it still works. Seems the two specializations are useless? Any comments?
template <> void f(int a) { g (a);}
template <> void f <int>(int a) { g (a);}
regards,
George
|
|
|
|
|
that are examples. when i found your examples of "template<class T> void f(int a)", i just given example if you meant specialisation.
George_George wrote: I have tried if we remove the following two functions
because you have generic functions with template argument T already.
George_George wrote: Seems the two specializations are useless?
It is useful if you write special code apart from generic function in the template specialization.
|
|
|
|
|
Thanks Rajkumar!
template <> void f(int a) { g (a);}
template <> void f <int>(int a) { g (a);}
Are the two above function definitons of the same function and just different coding style? Or they have differences?
regards,
George
|
|
|
|
|
I think both are same explicit specialization as if you define both, induce redefinition error, literally, the former template argument is deduced while the later template argument is specified.
|
|
|
|
|
Thanks Rajkumar,
I have got the redefinition error as you pointed out. Cool! But to use the latter one, it is also not mandatory needed to specify the template parameter type, it could also be deduced. Here is my code to prove. Any comments?
#include <iostream>
using namespace std;
void g(int a){ cout << a << endl;}
template <class T> void f(T a) { g (a);}
template <> void f <int>(int a) { g (a);}
int main()
{
f (100);
return 0;
}
regards,
George
|
|
|
|
|
Hello everyone,
As mentioned in Gotw about deque. What means "streamlined version of deque with simplified iterators"? I did some search and can not find out.
http://www.gotw.ca/gotw/054.htm
--------------------
Finally, note that the popular library implementation that I tested has since been revised and now includes a streamlined version of deque with simplified iterators. I do not yet have a copy of that library, but it will be interesting to see how much of the deque disadvantage in even the raw iterator "traverse" and element-accessing "at" tests will remain compared to vector.
--------------------
thanks in advance,
George
|
|
|
|
|
it simply means there is a STL library somewhere that even Sutter does not yet have (at the time of the writing) contains a streamline version of the dequeue container and simplified iterator.
streamline means better (either in performance and/or in memory useage).
simplified means, well, more simple than the current implementation (I assume the current one is not simple)
I also assume that you know that the "Guru Of The Week" #54 is nearly 19 years old (written in 1999) and that there have been lots of improvements in STL and that what he was writing about is probably already included in the current STL implementation and that there are no "non" streamline or "non" simplified versions of dequeue.
you could try to get a copy of the dequeue code from that time and compare it with the current code to see what have changed.
|
|
|
|
|
Thanks Maximilien!
1.
What is your point? The Gotw #54 is out of date?
2.
I want to learn your comments about what do you think are possible improvements? compare with traditional implementation of deque? Currently, I only know traditional way of implementing deque -- chunks and chunks containing elements.
regards,
George
|
|
|
|
|
Hi,
I just want to ask how can i select folder in file open dialog?
Because it usually enters a folder when i select it.
Is it possible?
If not what should i use and how will i do it?
Thank you.
|
|
|
|
|