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

template blankets name of base class function

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
* 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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.