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

P

Pallav singh

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
 
V

Victor Bazarov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top