Error :: specialization of Template Function on Meta - Template Class

Discussion in 'C++' started by Pallav singh, Jan 6, 2012.

  1. Pallav singh

    Pallav singh Guest

    Hi All ,

    i am getting Error while specialization of Template Function on Meta -
    Template Class
    Thanking in Advance.

    ------------------ file1.h -----------------------------------
    template <typename T>
    class Foo {
    T data;
    public:
    Foo(T in);

    template <typename TT> void bar(TT value);
    };

    =========== End of File =============
    -------------------------file1.hpp -----------------------------

    #include "file1.h"

    #include <iostream>
    #include <string>
    using namespace std;

    template <typename T>
    Foo<T> :: Foo(T input) : data(input) { }

    template <typename T>
    template <typename TT>
    void Foo<T> :: bar(TT value)
    {
    cout << " Foo<T> :: void bar(TT value) : " << value << endl;
    }


    template <typename T>
    void Foo<T> :: bar<string>(string value)
    {
    cout << " Foo<T> :: String Specialization : " << value << endl;
    }

    =============================================

    ----------------------------------- file1.cpp
    ----------------------------------------

    #include "file1.h"

    #include <iostream>
    using namespace std;

    template <>
    class Foo<int> {
    int data;

    public:
    Foo(int in) : data(in) { }

    template<typename TT>
    void bar(TT value) {
    cout << "default: " << static_cast<int>(value)*data << endl;
    }

    };

    template <>
    void Foo<int> :: bar(double value) {
    cout << "special: " << value*data << endl;
    }

    ==========================================

    ----------------------- ExportFile.cc
    --------------------------------------
    #include "file1.hpp"

    #include <string>
    using namespace std;

    //////// Specialization part //////////
    template class Foo<int>;

    template class Foo<string>;
    template class Foo<float>;
    template class Foo<char>;


    // Explicit generation of Function Symbol
    // Explicit Instantation of Function inside Class

    template void Foo<char> :: bar<char> ( char );
    template void Foo<int> :: bar<int> ( int );
    template void Foo<float> :: bar<float> ( float );
    template void Foo<string> :: bar<string> ( string )

    ================================================

    $ g++ -c -g file1.cc ExportFile.cc
    In file included from ExportFile.cc:1:0:
    file1.hpp:19:41: error: template-id `bar<std::string>' in declaration
    of primary
    template
    file1.hpp:19:7: error: prototype for `void Foo<T>::bar(std::string)'
    does not ma
    tch any in class `Foo<T>'
    file1.h:7:45: error: candidate is: template<class T> template<class
    TT> void Foo
    ::bar(TT)


    Thanks
    Pallav Singh
     
    Pallav singh, Jan 6, 2012
    #1
    1. Advertising

  2. Re: Error :: specialization of Template Function on Meta - TemplateClass

    On 1/6/2012 1:18 PM, Pallav singh wrote:
    > Hi All ,
    >
    > i am getting Error while specialization of Template Function on Meta -
    > Template Class
    > Thanking in Advance.
    >
    > ------------------ file1.h -----------------------------------
    > template<typename T>
    > class Foo {
    > T data;
    > public:
    > Foo(T in);
    >
    > template<typename TT> void bar(TT value);
    > };
    >
    > =========== End of File =============
    > -------------------------file1.hpp -----------------------------
    >
    > #include "file1.h"
    >
    > #include<iostream>
    > #include<string>
    > using namespace std;
    >
    > template<typename T>
    > Foo<T> :: Foo(T input) : data(input) { }
    >
    > template<typename T>
    > template<typename TT>
    > void Foo<T> :: bar(TT value)
    > {
    > cout<< " Foo<T> :: void bar(TT value) : "<< value<< endl;
    > }
    >
    >
    > template<typename T>
    > void Foo<T> :: bar<string>(string value)
    > {
    > cout<< " Foo<T> :: String Specialization : "<< value<< endl;
    > }
    >
    > =============================================
    >
    > ----------------------------------- file1.cpp
    > ----------------------------------------
    >
    > #include "file1.h"
    >
    > #include<iostream>
    > using namespace std;
    >
    > template<>
    > class Foo<int> {
    > int data;
    >
    > public:
    > Foo(int in) : data(in) { }
    >
    > template<typename TT>
    > void bar(TT value) {
    > cout<< "default: "<< static_cast<int>(value)*data<< endl;
    > }
    >
    > };
    >
    > template<>
    > void Foo<int> :: bar(double value) {
    > cout<< "special: "<< value*data<< endl;
    > }
    >
    > ==========================================
    >
    > ----------------------- ExportFile.cc
    > --------------------------------------
    > #include "file1.hpp"
    >
    > #include<string>
    > using namespace std;
    >
    > //////// Specialization part //////////
    > template class Foo<int>;
    >
    > template class Foo<string>;
    > template class Foo<float>;
    > template class Foo<char>;
    >
    >
    > // Explicit generation of Function Symbol
    > // Explicit Instantation of Function inside Class
    >
    > template void Foo<char> :: bar<char> ( char );
    > template void Foo<int> :: bar<int> ( int );
    > template void Foo<float> :: bar<float> ( float );
    > template void Foo<string> :: bar<string> ( string )
    >
    > ================================================
    >
    > $ g++ -c -g file1.cc ExportFile.cc
    > In file included from ExportFile.cc:1:0:
    > file1.hpp:19:41: error: template-id `bar<std::string>' in declaration
    > of primary
    > template
    > file1.hpp:19:7: error: prototype for `void Foo<T>::bar(std::string)'
    > does not ma
    > tch any in class `Foo<T>'
    > file1.h:7:45: error: candidate is: template<class T> template<class
    > TT> void Foo
    > ::bar(TT)


    You cannot specialize a member template without first specializing the
    class template. As soon as you wrote

    template<class T> void Foo<T>::bar<string>

    you've violated that rule.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 6, 2012
    #2
    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. Dave
    Replies:
    4
    Views:
    7,708
    pdixtl
    Jun 4, 2010
  2. Ruben Campos
    Replies:
    3
    Views:
    6,491
  3. Joseph Turian
    Replies:
    4
    Views:
    608
    John Carson
    Mar 20, 2006
  4. Joseph Turian
    Replies:
    2
    Views:
    480
  5. Replies:
    2
    Views:
    351
Loading...

Share This Page