1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
class Bar
{
public:
void DoSomething() {}
};
template <typename T>
class Foo :public T
{
public:
void DoSomeMore()
{
DoSomething(2); //Won't pass Template syntax check - See Note 1
T t;
DoSomething(t); //Will pass template syntax check - See Note 2
T::DoSomething(4); //Will pass template syntax check - See Note 2
}
};
|
The reasoning go something like this:
Note 1.
The
DoSomething(2) function call does NOT depend
on any of the Foo template parameters ( in other words the template parameter T is not involved)
Therefore it is a standard C++ function call - which means that the function itself or
the declaration
MUST already be known.
Given That Foo itself does not contain the function, and the base class type is NOT known
at this time because the base class depends on the template parameter T,
then we get the error.
Note 2.
Both these function calls invlve the template parameter T.
Although T is not resolved at the initial template checks, their syntax is correct
so they will pass the template syntax checks.
However, when you try to instatntiate an actual concrete version
of Foo - they could well fail depending on what type T turns out to be.
1 2 3 4 5 6
|
Foo<int> fi; //complete failure - cannot be instatiated at all.
Foo<std::string>fs ; //will instantiate OK - but
fs.DoSomeMore(); //compile time failure on any of the 3 function calls
Foo<Bar>fb; //instantiate ok
fb.DoSomeMore(); //will only work for T::DoSomething(4), compile time error on other 2;
|
EDIT
I really should mention the
using declaration and class members
to resolve some of the problems - so I'll update later.