473,669 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template instantiation and typedefs.

Any idea why the following code does not compile?

----
#include<iostre am>
#include<list>
using namespace std;

class Base {
public:
int val;
Base(int i):val(i){}
void show(){cout<<na me()<<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*>::itera tor a_iter;
typedef list<B*>::itera tor b_iter;
typedef pair<a_iter,a_i tera_seq;
typedef pair<b_iter,b_i terb_seq;

template <class TBase* show(pair<typen ame list<T*>::itera tor,
typename list<T*>::itera torseq, int val){
for(typename list<T*>::itera tor 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_bac k(new A(i));
b_list.push_bac k(new B(i));
}
a_seq a = make_pair(a_lis t.begin(),a_lis t.end());
b_seq b = make_pair(b_lis t.begin(),b_lis t.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 2221

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

----
#include<iostre am>
#include<list>
using namespace std;

class Base {
public:
int val;
Base(int i):val(i){}
void show(){cout<<na me()<<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*>::itera tor a_iter;
typedef list<B*>::itera tor b_iter;
typedef pair<a_iter,a_i tera_seq;
typedef pair<b_iter,b_i terb_seq;

template <class TBase* show(pair<typen ame list<T*>::itera tor,
typename list<T*>::itera torseq, int val){
for(typename list<T*>::itera tor 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_bac k(new A(i));
b_list.push_bac k(new B(i));
}
a_seq a = make_pair(a_lis t.begin(),a_lis t.end());
b_seq b = make_pair(b_lis t.begin(),b_lis t.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.co m wrote:
Any idea why the following code does not compile?

----
#include<iostre am>
#include<list>
using namespace std;

class Base {
public:
int val;
Base(int i):val(i){}
void show(){cout<<na me()<<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*>::itera tor a_iter;
typedef list<B*>::itera tor b_iter;
typedef pair<a_iter,a_i tera_seq;
typedef pair<b_iter,b_i terb_seq;

template <class TBase* show(pair<typen ame list<T*>::itera tor,
typename list<T*>::itera torseq, int val){
for(typename list<T*>::itera tor 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_bac k(new A(i));
b_list.push_bac k(new B(i));
}
a_seq a = make_pair(a_lis t.begin(),a_lis t.end());
b_seq b = make_pair(b_lis t.begin(),b_lis t.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.co m wrote:
Any idea why the following code does not compile?
....
template <class TBase* show(pair<typen ame list<T*>::itera tor,
typename list<T*>::itera torseq, 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
2011
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 instantiation of foo:
5
1359
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
2474
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> registrar; }; The constructor of Registrar does the registering when it is initialized.
4
1645
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
1741
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
2613
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 an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
2
2011
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. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
3
2729
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 template class called "myTemplate", for which there is an instantiation for 'int' type. Now suppose a class in module B binary wants to use this template
4
1593
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 try to expose only one function of my class its failing #include <iostream>
0
8465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8895
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8809
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6210
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.