By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,603 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,603 IT Pros & Developers. It's quick & easy.

question about Partial Template Specialization

P: n/a
I have some code like following:
template<class T>
class A
{
public:
A(T& t) : m_b(t){};
void Func()
{
m_b.DoSome();
};
private:
B<tm_b;
};
---------------------------------------
template<class T>
class B
{
public:
void DoSome() {
cout << "type t";
};
private:
T &t;
};
template<>
void B<int>::DoSome()
{
cout << "type int";
}
---------------------------------------
main()
{
A<inta;
a.Func();
}

the output is "type t" instead of "type int", why?! any help is
appericiated.

Nov 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Leon Wu wrote:
I have some code like following:
template<class T>
class A
{
public:
A(T& t) : m_b(t){};
void Func()
{
m_b.DoSome();
};
private:
B<tm_b;
};
---------------------------------------
template<class T>
class B
{
public:
void DoSome() {
cout << "type t";
};
private:
T &t;
};
template<>
void B<int>::DoSome()
{
cout << "type int";
}
---------------------------------------
main()
{
A<inta;
a.Func();
}

the output is "type t" instead of "type int", why?! any help is
appericiated.
Please post a complete compilable program...

After fixing a number of compile errors you get this -
this says "type int"

#include <iostream>

template<class T>
class B;

template<class T>
class A
{
public:
A() {};
A(T& t) : m_b(t){};
void Func()
{
m_b.DoSome();
};
private:
B<Tm_b;
};
//---------------------------------------
template<class T>
class B
{
public:
void DoSome() {
std::cout << "type t";
};
private:
T &t;
};
template<>
void B<int>::DoSome()
{
std::cout << "type int";
}
//---------------------------------------
int main()
{
A<inta;
a.Func();
}
Nov 13 '06 #2

P: n/a
I write this code snipet according to my program, after test it, I find
it work well too.
after some investigation, I found the different between my program and
this code snipet is
all class is inside different head file. just as follows:
main.cpp-------------------------------------
#include <iostream>
#include "A.h"
int main()
{
int c = 10;
A<inta(c);
a.Func();
return 0;
}
A.h---------------------------
#ifndef __RCBB_A_H__
#define __RCBB_A_H__
#include "b.h"
template<class T>
class A
{
public:
A(T& t) : m_b(t){};
void Func()
{
m_b.DoSome();
};
private:
B<Tm_b;
};

B.h----------------------------
#ifndef __RCBB_B_H__
#define __RCBB_B_H__
template<class T>
class B
{
public:
B(T& t) : m_t(t){};
void DoSome()
{
cout << "type t";
};
private:
T &m_t;
};
SpecB.h--------------------------
#ifndef __RCBB_SPECB_H__
#define __RCBB_SPECB_H__
#include <iostream>
#include "B.h"
template<>
void B<int>::DoSome()
{
std::cout << "type int";
}
----------------------------------------------------------------------------
If the code is like above, the output will be "type t".

"Gianni Mariani д
"
Leon Wu wrote:
I have some code like following:
template<class T>
class A
{
public:
A(T& t) : m_b(t){};
void Func()
{
m_b.DoSome();
};
private:
B<tm_b;
};
---------------------------------------
template<class T>
class B
{
public:
void DoSome() {
cout << "type t";
};
private:
T &t;
};
template<>
void B<int>::DoSome()
{
cout << "type int";
}
---------------------------------------
main()
{
A<inta;
a.Func();
}

the output is "type t" instead of "type int", why?! any help is
appericiated.

Please post a complete compilable program...

After fixing a number of compile errors you get this -
this says "type int"

#include <iostream>

template<class T>
class B;

template<class T>
class A
{
public:
A() {};
A(T& t) : m_b(t){};
void Func()
{
m_b.DoSome();
};
private:
B<Tm_b;
};
//---------------------------------------
template<class T>
class B
{
public:
void DoSome() {
std::cout << "type t";
};
private:
T &t;
};
template<>
void B<int>::DoSome()
{
std::cout << "type int";
}
//---------------------------------------
int main()
{
A<inta;
a.Func();
}
Nov 14 '06 #3

P: n/a
The reason you are not seeing the specialization work as you expect
appears to be that you have not included in the header file that
contains your specialization. That header must be included too so the
compiler will know there is a specialization.

Nov 14 '06 #4

P: n/a
Thank you. I solve this problem, now I put the declaration of
specialization in B.h, and move the definition into a cpp, it work
well.
"Ivan Novick д
"
The reason you are not seeing the specialization work as you expect
appears to be that you have not included in the header file that
contains your specialization. That header must be included too so the
compiler will know there is a specialization.
Nov 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.