Template member functions allow us to parameterise functions independently for a class they belong to. They are supported for both template and non-template classes and it worked fine if we directly call it from class object. But in case if class is composed-off in other class c++ compiler fail to resolve it. In my case I am able to call template member function directly from object but it fails( MsgParser::run(){source.showType();}) when object is composition. logic behind it ?
struct A{};
struct B{};
class DataSource{
public:
template<class Type>
void showType(){
std::cout<<std::endl<<typeid(Type).name();
}
void disp(){ std::cout<<std::endl<<"All OK";}
private:
};
template<class Source>
class MsgParser{
public:
MsgParser(Source &s):source(s){}
void run(){
source.disp();
source.showType<A>(); // failed to call ??
}
private:
Source &source;
};
int main()
{
DataSource dataSource;
dataSource.showType<A>();
MsgParser<DataSource> msgParser(dataSource);
msgParser.run();
return 1;
}