473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

template instantiation and typedefs.

Any idea why the following code does not compile?

----
#include<iostream>
#include<list>
using namespace std;

class Base {
public:
int val;
Base(int i):val(i){}
void show(){cout<<name()<<val<<endl;}
virtual string name() = 0;
};
class A: public Base{
public:
A(int i):Base(i){}
string name() { return "a";}
};

class B: public Base{
public:
B(int i):Base(i){}
string name() { return "b";}
};

typedef list<A*>::iterator a_iter;
typedef list<B*>::iterator b_iter;
typedef pair<a_iter,a_itera_seq;
typedef pair<b_iter,b_iterb_seq;

template <class TBase* show(pair<typename list<T*>::iterator,
typename list<T*>::iteratorseq, int val){
for(typename list<T*>::iterator i = seq.first; i!=seq.second; ++i)
if(i->val==val) i->show();
}

int main(){
list<A*a_list;
list<B*b_list;
for(int i=0;i<10;i++){
a_list.push_back(new A(i));
b_list.push_back(new B(i));
}
a_seq a = make_pair(a_list.begin(),a_list.end());
b_seq b = make_pair(b_list.begin(),b_list.end());
show(a, 5);
show(b, 6);
}
---

I get the following compilation errors.
---
test.cpp: In function `int main()':
test.cpp:43: no matching function for call to `show(a_seq&, int)'
test.cpp:44: no matching function for call to `show(b_seq&, int)'
--

both a_seq and b_seq are typedefed to pairs of list iterator. am i
missing something?

thanks,
--
ajd.

Dec 8 '06 #1
3 2209

ai****@gmail.com wrote:
Any idea why the following code does not compile?

----
#include<iostream>
#include<list>
using namespace std;

class Base {
public:
int val;
Base(int i):val(i){}
void show(){cout<<name()<<val<<endl;}
virtual string name() = 0;
};
class A: public Base{
public:
A(int i):Base(i){}
string name() { return "a";}
};

class B: public Base{
public:
B(int i):Base(i){}
string name() { return "b";}
};

typedef list<A*>::iterator a_iter;
typedef list<B*>::iterator b_iter;
typedef pair<a_iter,a_itera_seq;
typedef pair<b_iter,b_iterb_seq;

template <class TBase* show(pair<typename list<T*>::iterator,
typename list<T*>::iteratorseq, int val){
for(typename list<T*>::iterator i = seq.first; i!=seq.second; ++i)
if(i->val==val) i->show();
}

int main(){
list<A*a_list;
list<B*b_list;
for(int i=0;i<10;i++){
a_list.push_back(new A(i));
b_list.push_back(new B(i));
}
a_seq a = make_pair(a_list.begin(),a_list.end());
b_seq b = make_pair(b_list.begin(),b_list.end());
show(a, 5);
show(b, 6);
}
---

I get the following compilation errors.
---
test.cpp: In function `int main()':
test.cpp:43: no matching function for call to `show(a_seq&, int)'
test.cpp:44: no matching function for call to `show(b_seq&, int)'
--

both a_seq and b_seq are typedefed to pairs of list iterator. am i
missing something?
If you try it without the convenient typedefs, you'll find it still
doesn't work.

The standard refers to two contexts in which template argument
deduction is impossible for function templates. One of these is the
situation you're encountering: inside the qualification part of a
qualified identifier (that is, you can't deduce T from A<T>::B).

The other case is when you try to deduce a template argument from
another template id that uses the template parameter-to-be-deduced
within an expression; say:

template<int istruct B {
static const int value = i;
};

template<int ivoid bfunc(B<i-2&b) {}

int main(void)
{
B<10b;
bfunc(b);
}

The implementation can't deduce that it should call bfunc<12>(b).

Dec 8 '06 #2

ai****@gmail.com wrote:
Any idea why the following code does not compile?

----
#include<iostream>
#include<list>
using namespace std;

class Base {
public:
int val;
Base(int i):val(i){}
void show(){cout<<name()<<val<<endl;}
virtual string name() = 0;
};
class A: public Base{
public:
A(int i):Base(i){}
string name() { return "a";}
};

class B: public Base{
public:
B(int i):Base(i){}
string name() { return "b";}
};

typedef list<A*>::iterator a_iter;
typedef list<B*>::iterator b_iter;
typedef pair<a_iter,a_itera_seq;
typedef pair<b_iter,b_iterb_seq;

template <class TBase* show(pair<typename list<T*>::iterator,
typename list<T*>::iteratorseq, int val){
for(typename list<T*>::iterator i = seq.first; i!=seq.second; ++i)
if(i->val==val) i->show();
}

int main(){
list<A*a_list;
list<B*b_list;
for(int i=0;i<10;i++){
a_list.push_back(new A(i));
b_list.push_back(new B(i));
}
a_seq a = make_pair(a_list.begin(),a_list.end());
b_seq b = make_pair(b_list.begin(),b_list.end());
show(a, 5);
show(b, 6);
}
---

I get the following compilation errors.
---
test.cpp: In function `int main()':
test.cpp:43: no matching function for call to `show(a_seq&, int)'
test.cpp:44: no matching function for call to `show(b_seq&, int)'
--

both a_seq and b_seq are typedefed to pairs of list iterator. am i
missing something?

thanks,
--
ajd.
show(pair,val) with no template type is in use together with an
irrelevent return to the base which should be empty.
I guess pointers used might also sicken your program, mightnot they ?

Dec 8 '06 #3
ai****@gmail.com wrote:
Any idea why the following code does not compile?
....
template <class TBase* show(pair<typename list<T*>::iterator,
typename list<T*>::iteratorseq, int val){
The compiler can't deduce a dependant type.

....
both a_seq and b_seq are typedefed to pairs of list iterator. am i
missing something?

Think about it for a second. The compiler would need to check every
possible specialization of that template class (which is infinite) to
deduce.

One methodology I use is:

template <typename TBase * show( const T & a, int b, check<Tc = 0 );

check<Tis a compile time assertion for the right type of T when
instantiated.

When deducing template function arguments, if the compiler envounters an
error instatitaing a parameter type, that specialization is excluded.
Dec 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
5
by: Alexander Stippler | last post by:
Hello, I wrote an (templatized) operator==() and don't know why the compiler doesn't consider it. What prevents it from being chosen? class Data {}; template <typename D> class Vector {
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
4
by: Dave | last post by:
Hello all, Consider this template: template <typename T> void foo(T bar) {...} Here are three ways to instantiate this: 1.
4
by: Peter Ammon | last post by:
I have a template class: #include <cassert> #include <algorithm> template <class T, int N> class Point { private: T components;
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
3
by: sks | last post by:
Hello all Is the usage of extern keyword valid for telling the compiler to NOT instantiate a template and to link it from an another binary? For example: Suppose module A's binary contains a...
4
by: Pallav singh | last post by:
Hi All, i am getting error during explicit function Instantiation for a class Template if i do explicit Instantiation of class it work and all function symbol i get in object file But if i...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.