Use of template function instantiation

Discussion in 'C++' started by Alex Vinokur, Dec 12, 2009.

  1. Alex Vinokur

    Alex Vinokur Guest

    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 .
    Alex Vinokur, Dec 12, 2009
    #1
    1. Advertising

  2. Alex Vinokur

    feverzsj Guest

    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/templatesourceorg.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.
    feverzsj, Dec 12, 2009
    #2
    1. Advertising

  3. Alex Vinokur

    Jorgen Grahn Guest

    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 .
    Jorgen Grahn, Dec 12, 2009
    #3
  4. Alex Vinokur

    Daniel Pitts Guest

    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/>
    Daniel Pitts, Dec 12, 2009
    #4
  5. Alex Vinokur

    Jorgen Grahn Guest

    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 .
    Jorgen Grahn, Dec 14, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fernando Cuenca
    Replies:
    4
    Views:
    2,522
    Gianni Mariani
    Sep 6, 2004
  2. Thomas Maier-Komor
    Replies:
    6
    Views:
    614
    Thomas Maier-Komor
    May 19, 2005
  3. Replies:
    1
    Views:
    571
    Salt_Peter
    Dec 25, 2006
  4. Ed
    Replies:
    1
    Views:
    342
  5. Noah Roberts
    Replies:
    6
    Views:
    1,152
    Johannes Schaub (litb)
    Feb 2, 2011
Loading...

Share This Page