Explicit specialization syntax

Discussion in 'C++' started by Ney André de Mello Zunino, Mar 11, 2011.

  1. Hello.

    I'm trying to use that template specialization "trick" to allow one to
    keep template implementations separate from the header file. However, I
    am having trouble with the right syntax --I get a linker error when I
    try to instantiate a Foo<int> on main(). Could anybody help?

    // ======================== Foo.h ========================
    #ifndef FOO_H__
    #define FOO_H__

    template<typename Type>
    class Foo {
    public:
    Foo(Type object);
    };

    #endif
    // =======================================================


    // ======================= Foo.cpp =======================
    #include "Foo.h"
    #include <iostream>

    template<typename Type>
    Foo<Type>::Foo(Type object) {
    std::cout << "Building Foo with object: " << object << "\n";
    }

    template<> Foo<int>::Foo(int);
    // =======================================================


    // ====================== Main.cpp =======================
    #include "Foo.h"

    int main() {
    Foo<int> foo(10);
    }
    // =======================================================

    Thank you,

    --
    Ney André de Mello Zunino
    Ney André de Mello Zunino, Mar 11, 2011
    #1
    1. Advertising

  2. Ney André de Mello Zunino wrote:
    > // ======================= Foo.cpp =======================
    > #include "Foo.h"
    > #include <iostream>
    >
    > template<typename Type>
    > Foo<Type>::Foo(Type object) {
    > std::cout << "Building Foo with object: " << object << "\n";
    > }
    >
    > template<> Foo<int>::Foo(int);
    > // =======================================================


    I don't know what you are going to achieve. But the above line
    /declares/ a constructor of Foo<int>. There is no definition for it.

    Did you mean

    template<> Foo<int>::Foo(int)
    { std::cout << "Building Foo with object: " << object << "\n";
    }


    Marcel
    Marcel Müller, Mar 11, 2011
    #2
    1. Advertising

  3. On 3/11/2011 1:08 PM, Ney André de Mello Zunino wrote:
    > Hello.
    >
    > I'm trying to use that template specialization "trick" to allow one to
    > keep template implementations separate from the header file. However, I
    > am having trouble with the right syntax --I get a linker error when I
    > try to instantiate a Foo<int> on main(). Could anybody help?
    >
    > // ======================== Foo.h ========================
    > #ifndef FOO_H__
    > #define FOO_H__
    >
    > template<typename Type>
    > class Foo {
    > public:
    > Foo(Type object);
    > };
    >
    > #endif
    > // =======================================================
    >
    >
    > // ======================= Foo.cpp =======================
    > #include "Foo.h"
    > #include <iostream>
    >
    > template<typename Type>
    > Foo<Type>::Foo(Type object) {
    > std::cout << "Building Foo with object: " << object << "\n";
    > }
    >
    > template<> Foo<int>::Foo(int);


    An explicit instantiation (not specialization) is what you're looking
    for, I think. Try

    template Foo<int>::Foo(int);

    > // =======================================================
    >
    >
    > // ====================== Main.cpp =======================
    > #include "Foo.h"
    >
    > int main() {
    > Foo<int> foo(10);
    > }
    > // =======================================================
    >
    > Thank you,
    >


    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 11, 2011
    #3
  4. Em 11/03/2011 16:08, Victor Bazarov escreveu:

    > An explicit instantiation (not specialization) is what you're looking
    > for, I think. Try
    >
    > template Foo<int>::Foo(int);


    Victor,

    That did it. And you're right; I meant instantiation.

    Now, for a follow-up question. Would it be possible to explicitly
    instantiate the whole Foo class template instead of only its
    constructor? What would the syntax look like?

    Thanks again,

    --
    Ney André de Mello Zunino
    Ney André de Mello Zunino, Mar 11, 2011
    #4
  5. Em 11/03/2011 16:38, Ney André de Mello Zunino escreveu:

    > Now, for a follow-up question. Would it be possible to explicitly
    > instantiate the whole Foo class template instead of only its
    > constructor? What would the syntax look like?


    I have just tried the following explicit instantiation:

    template class Foo<int>;

    and it worked. I then tried adding another member function to the class
    template, implemented it on the separate Foo.cpp file and tried invoking
    it from main. No linker errors.

    // ======= In Foo.h:

    template<typename Type>
    class Foo {
    public:
    Foo(Type object);
    void bar(Type object) const; // new member function
    };

    // ======= In Foo.cpp:

    template<typename Type>
    void Foo<Type>::bar(Type object) const {
    std::cout << "Foo<Type> invoked with object: " << object << "\n";
    }

    // ======= In Main.cpp:

    int main() {
    Foo<int> foo(10);
    foo.bar(20);
    }

    P.S.: Sorry for I should have made those attempts before asking.

    --
    Ney André de Mello Zunino
    Ney André de Mello Zunino, Mar 11, 2011
    #5
  6. On 3/11/2011 3:01 PM, Ney André de Mello Zunino wrote:
    > Em 11/03/2011 16:38, Ney André de Mello Zunino escreveu:
    >
    >> Now, for a follow-up question. Would it be possible to explicitly
    >> instantiate the whole Foo class template instead of only its
    >> constructor? What would the syntax look like?

    >
    > I have just tried the following explicit instantiation:
    >
    > template class Foo<int>;
    >
    > and it worked. I then tried adding another member function to the class
    > template, implemented it on the separate Foo.cpp file and tried invoking
    > it from main. No linker errors.
    >
    > // ======= In Foo.h:
    >
    > template<typename Type>
    > class Foo {
    > public:
    > Foo(Type object);
    > void bar(Type object) const; // new member function
    > };
    >
    > // ======= In Foo.cpp:
    >
    > template<typename Type>
    > void Foo<Type>::bar(Type object) const {
    > std::cout << "Foo<Type> invoked with object: " << object << "\n";
    > }


    I am guessing the c-tor and the explicit instantiation are here
    somewhere, as well... :)

    >
    > // ======= In Main.cpp:
    >
    > int main() {
    > Foo<int> foo(10);
    > foo.bar(20);
    > }
    >
    > P.S.: Sorry for I should have made those attempts before asking.
    >


    No harm done.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 11, 2011
    #6
  7. Ney André de Mello Zunino

    Richard Guest

    [Please do not mail me a copy of your followup]

    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    <> spake the secret code
    <ildtn9$lmf$> thusly:

    >Em 11/03/2011 16:08, Victor Bazarov escreveu:
    >
    >> An explicit instantiation (not specialization) is what you're looking
    >> for, I think. Try
    >>
    >> template Foo<int>::Foo(int);

    >
    >Victor,
    >
    >That did it. And you're right; I meant instantiation.


    This is also covered in the FAQ. There's lots of really useful
    information there, I would recommend familiarizing yourself with the
    entire FAQ on at least a skimming level.

    [35.13] How can I avoid linker errors with my template functions?
    <http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13>
    --
    "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
    <http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

    Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
    Richard, Mar 11, 2011
    #7
    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. Andriy Shnyr
    Replies:
    3
    Views:
    751
    Larry Evans
    Dec 4, 2003
  2. J.T. Conklin
    Replies:
    1
    Views:
    428
    David Hilsee
    Aug 11, 2004
  3. Patrick Kowalzick
    Replies:
    0
    Views:
    799
    Patrick Kowalzick
    Oct 29, 2004
  4. Andy
    Replies:
    5
    Views:
    494
    Shezan Baig
    Jan 30, 2005
  5. Replies:
    1
    Views:
    559
    Salt_Peter
    Dec 25, 2006
Loading...

Share This Page