Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Use of template function instantiation

Reply
Thread Tools

Use of template function instantiation

 
 
Alex Vinokur
Guest
Posts: n/a
 
      12-12-2009
When is it necessary to use template function instantiation?

template<typename T> void foo(T) { }

template void foo<int> (int); // Instantiation with the explicitly
specified template.

template void foo(char); // Instantiation with the deduced
template argument 'char'.

template void foo(double d) // Specialization
{
// Specialization
}

int main()
{
foo(123); // int
foo ('a'); // char
foo (3.14); // double - specialization
foo (std::string());
return 0;
}

What is the difference here between calling foo(123), foo (3.14), foo
(std::string()) ?

Thanks,

Alex .

 
Reply With Quote
 
 
 
 
feverzsj
Guest
Posts: n/a
 
      12-12-2009
Alex Vinokur wrote:
> When is it necessary to use template function instantiation?


You mean explicit instantiation?
The case one using explicit instantiation is when compiler takes too
long time to instantiate the template in place for many times.
To achieve this, you have to manage the source code carefully, which
I think is not worth the candle.
Here contains some examples, http://www.codeproject.com/KB/cpp/te...sourceorg.aspx

> template<typename T> void foo(T) { }
>
> template void foo<int> (int); *// *Instantiation with the explicitly
> specified template.
>
> template void foo(char); * * * * * * // Instantiation with the deduced
> template argument 'char'.
>
> template void foo(double d) * // Specialization
> {
> * // Specialization
>
> }
>
> int main()
> {
> * *foo(123); * // int
> * *foo ('a'); * * // char
> * *foo (3.14); * // double - specialization
> * *foo (std::string());
> * *return 0;
>
> }
>
> What is the difference here between calling foo(123), foo (3.14), foo
> (std::string()) ?


Calling different instantiations, but your usage of explicit
instantiation is useless. Still check the web page above.
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-12-2009
On Sat, 2009-12-12, feverzsj wrote:
> Alex Vinokur wrote:
>> When is it necessary to use template function instantiation?

>
> You mean explicit instantiation?
> The case one using explicit instantiation is when compiler takes too
> long time to instantiate the template in place for many times.


I've only used explicit instantiation once, and it was not the case
you describe above (which you think is the *only* case, right?).

My case was simple: I started out with a class

class Foo {
...
void bar(const A&);
void bar(const B&);
void bar(const C&);
};

with the three Foo::bar() very similar, and A, B, C with similar
interfaces but not related by inheritance (I dislike using inheritance
in cases where I don't need run-time polymorphism).

So that turned into:

class Foo {
...
template<class T> void bar(const T&);
};

with the template member defined in the implementation file, followed
by explicit instantiations for A, B and C. I expect I'll be adding a
D, E and F later.

Is this a common idiom, by the way? I "discovered" it by accident; I
don't have that much experience with templates.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      12-12-2009
Jorgen Grahn wrote:
> On Sat, 2009-12-12, feverzsj wrote:
>> Alex Vinokur wrote:
>>> When is it necessary to use template function instantiation?

>> You mean explicit instantiation?
>> The case one using explicit instantiation is when compiler takes too
>> long time to instantiate the template in place for many times.

>
> I've only used explicit instantiation once, and it was not the case
> you describe above (which you think is the *only* case, right?).
>
> My case was simple: I started out with a class
>
> class Foo {
> ...
> void bar(const A&);
> void bar(const B&);
> void bar(const C&);
> };
>
> with the three Foo::bar() very similar, and A, B, C with similar
> interfaces but not related by inheritance (I dislike using inheritance
> in cases where I don't need run-time polymorphism).
>
> So that turned into:
>
> class Foo {
> ...
> template<class T> void bar(const T&);
> };
>
> with the template member defined in the implementation file, followed
> by explicit instantiations for A, B and C. I expect I'll be adding a
> D, E and F later.

That is an interesting trick. Although I think I would hide that as an
implementation detail, keeping the template logic to Foo.cc, rather
than Foo.h
--- foo.h ---
class Foo {
...
void bar(const A&);
void bar(const B&);
void bar(const C&);
};

--- foo.cc ---

#include "foo.h"

template<typename T>
void handleBar(const T&t) {
// bar logic
}

void Foo::bar(const A&a) { handleBar(a); }
void Foo::bar(const B&b) { handleBar(b); }
void Foo::bar(const C&c) { handleBar(c); }

--- ---


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-14-2009
On Sat, 2009-12-12, Daniel Pitts wrote:
> Jorgen Grahn wrote:
>> On Sat, 2009-12-12, feverzsj wrote:
>>> Alex Vinokur wrote:
>>>> When is it necessary to use template function instantiation?
>>> You mean explicit instantiation?
>>> The case one using explicit instantiation is when compiler takes too
>>> long time to instantiate the template in place for many times.

>>
>> I've only used explicit instantiation once, and it was not the case
>> you describe above (which you think is the *only* case, right?).
>>
>> My case was simple: I started out with a class
>>
>> class Foo {
>> ...
>> void bar(const A&);
>> void bar(const B&);
>> void bar(const C&);
>> };
>>
>> with the three Foo::bar() very similar, and A, B, C with similar
>> interfaces but not related by inheritance (I dislike using inheritance
>> in cases where I don't need run-time polymorphism).
>>
>> So that turned into:
>>
>> class Foo {
>> ...
>> template<class T> void bar(const T&);
>> };
>>
>> with the template member defined in the implementation file, followed
>> by explicit instantiations for A, B and C. I expect I'll be adding a
>> D, E and F later.


> That is an interesting trick.


I thought so too, but maybe it just looks interesting and "tricky"
because there's so much focus on templates as a way to provide
infinite flexibility. Here I just used it to avoid copy&paste.

> Although I think I would hide that as an
> implementation detail, keeping the template logic to Foo.cc, rather
> than Foo.h
> --- foo.h ---
> class Foo {
> ...
> void bar(const A&);
> void bar(const B&);
> void bar(const C&);
> };
>
> --- foo.cc ---
>
> #include "foo.h"
>
> template<typename T>
> void handleBar(const T&t) {
> // bar logic
> }
>
> void Foo::bar(const A&a) { handleBar(a); }
> void Foo::bar(const B&b) { handleBar(b); }
> void Foo::bar(const C&c) { handleBar(c); }
>
> --- ---


That's even nicer. It doesn't matter *that* much in my real-life case.
Unlike in the A/B/C example it's fairly obvious which types can be
arguments to Foo::bar() -- they are all alone in a namespace and
correspond to requests in a networking protocol.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
Instantiation of template method in template class Ed C++ 1 08-27-2008 06:41 AM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
explicit instantiation of template methods of template classes Thomas Maier-Komor C++ 6 05-19-2005 08:00 AM
Explicit template instantiation from template function doesn't compile? Fernando Cuenca C++ 4 09-06-2004 04:06 PM



Advertisments