template blankets name of base class function

H

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:
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
 
A

Alf P. Steinbach

* 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.
 
H

Hartmut Sbosny

Alf said:
* 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
 
H

Hartmut Sbosny

Hartmut Sbosny schrieb:

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

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

class D : public B {

Hartmut
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top