473,396 Members | 1,804 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,396 software developers and data experts.

template blankets name of base class function

Hello NG,

I have a ordinary class `Base' with two functions `void foo()' and
`void any()':

class Base {
public:
Base() {}
void foo() {}
void any() {}
};

and I have a template `Templ<T>', which is derived from `Base', and defines
a function `void foo(T)', e.g. of the same name as the `Base' function, but
with different parameter types:

template <typename T>
class Templ : public Base
{
public:
Templ() {}
void foo(T t) {}
};

I had expected, that both `foo()'-functions should be distingushable. But
in fact the compiler does not resolve to Base::foo(), if I write `foo()' for
a `Templ' instance:

int main()
{
Templ<int> a;

a.any(); // OK --> Base::any()
a.foo(4); // OK --> Templ::foo(int)
a.foo(); // compiler *error*:
// no matching function for call to `Templ<int>::foo()'

((Base&)a).foo(); // OK --> Base::foo()
}

What is the reason for that?
I could imagine, that the compiler want to see a template parameter
dependency for all functions of a template instance, but if so, why
then `any()' is found?
Greetings
Hartmut
Nov 10 '05 #1
5 1619
* Hartmut Sbosny:
Hello NG,

I have a ordinary class `Base' with two functions `void foo()' and
`void any()':

class Base {
public:
Base() {}
void foo() {}
void any() {}
};

and I have a template `Templ<T>', which is derived from `Base', and defines
a function `void foo(T)', e.g. of the same name as the `Base' function, but
with different parameter types:

template <typename T>
class Templ : public Base
{
public:
using Base::foo;

Templ() {}
void foo(T t) {}
};


See the FAQ.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 10 '05 #2
Alf P. Steinbach schrieb:
* Hartmut Sbosny:

[...]

template <typename T>
class Templ : public Base
{
public:


using Base::foo;

Templ() {}
void foo(T t) {}
};


See the FAQ.


I skimed over the Template chapter before I asked here, but couldn't find
the right, now I'v read it carefully. I suppose, you refer to

[35.18] "Why am I getting errors when my template-derived-class accesses
something it inherited from its template-base-class?"

There the example is given,

template<typename T>
class B {
public:
void f() { }
};

template<typename T>
class D : public B<T> {
public:
void g()
{
f(); <-- compiler might give an error here
}
};

followed by the statement:

<cite FAQ>
Within D<T>::g(), the name f does not depend on template parameter T, so f
is known as a nondependent name. B<T>, on the other hand, is dependent on
template parameter T so B<T> is called a dependent name.

Here's the rule: the compiler does not look in dependent base classes (like
B<T>) when looking up nondependent names (like f).
</cite>

This suggests, that the "no looking up" takes place only in the case, where
a template is derived from another template, whereas in my case the
template was derived from an ordinary class.

Moreover, my compiler does not find exclusively such B[ase]-functions, those
names are re-used by the derived template (though with other argument
types), whereas the remaining B-functions are resolved.

The FAQ-comment "compiler might give an error here" makes me unsecure.
Does it mean, that in the case

class B {
public:
void f() {}
void g() {}
};

template<typename T>
class D : public B<T> {
public:
void f(int) {} // same name as a B-function, but different arg list
};

some compiler accept neither D<T>::g() nor D<T>::f(), some compiler accept
D<T>::g(), but not D<T>::f() (my case), and some compiler accept both?

BTW: the FAQ workaround "Change the call from f() to this->f()" doesn't work
with my compiler (GNU 3.3.4).

Well, I have a workaround now ('using B::f()' - Thanks!), but don't
understand after all, what the real problem is behind.

Best regards
Hartmut
Nov 10 '05 #3
Hartmut Sbosny schrieb:

Typo:
class B {
public:
void f() {}
void g() {}
};

template<typename T>
class D : public B<T> {


class D : public B {

Hartmut
Nov 10 '05 #4
Hartmut Sbosny wrote:
I skimed over the Template chapter before I asked here, but couldn't find
the right, now I'v read it carefully. I suppose, you refer to


Try this one:

http://www.parashift.com/c++-faq-lit....html#faq-23.9

Cheers! --M

Nov 10 '05 #5
mlimber schrieb:
Hartmut Sbosny wrote:
I skimed over the Template chapter before I asked here, but couldn't find
the right, now I'v read it carefully. I suppose, you refer to


Try this one:

http://www.parashift.com/c++-faq-lit....html#faq-23.9


Good tip, thank you!

Cheers, Hartmut
Nov 10 '05 #6

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

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
8
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class)...
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
2
by: Javier | last post by:
Hi Everyone, I have a dynamic checkbox in a datagrid that uses the ITemplate interface and has the checkchanged event wired up. When the checkbox is checked, the event event handler that...
3
by: aiooua | last post by:
Any idea why the following code does not compile? ---- #include<iostream> #include<list> using namespace std; class Base { public: int val;
12
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able...
5
by: lobequadrat | last post by:
Hello, I am trying to get the following code work (unfortunately not mine ... :( ) template <class Tclass Test { public: class ELEM;
4
by: Jim Langston | last post by:
This should illistrate what I am trying to do: template <class T> T SomeFunction( T parm ) { return parm; } template <class T> class SomeClass
9
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.