Wow. C ++ nu încetează niciodată să mă surprindă cu ciudățenia ei.
Într-o definiție de șablon, numele necalificate nu vor mai găsi membrii unei baze dependente (așa cum este specificat de [temp.dep]/3 în standardul C ++). De exemplu,
template struct B {
int m;
int n;
int f ();
int g ();
};
int n;
int g ();
template struct C : B {
void h ()
{
m = 0;//error
f (); //error
n = 0;//::n is modified
g (); //::g is called
}
};
You must make the names dependent, e.g. by prefixing them with this->. Here is the corrected definition of C::h,
template void C::h ()
{
this->m = 0;
this->f ();
this->n = 0
this->g ();
}
As an alternative solution (unfortunately not backwards compatible with GCC 3.3), you may use using declarations instead of this->:
template struct C : B {
using B::m;
using B::f;
using B::n;
using B::g;
void h ()
{
m = 0;
f ();
n = 0;
g ();
}
};
Asta e tot felul de nebuni. Mulțumesc, David.
Iată secțiunea "temp.dep/3" din standardul [ISO/IEC 14882: 2003] la care se referă:
În definiția unui șablon de clasă sau a unui membru al unui șablon de clasă, dacă o clasă de bază a șablonului de clasă depinde de un parametru de șablon, domeniul de bază al clasei de bază nu este examinat în timpul căutării necalificate a numelui, fie în punctul de definiție a șablonului de clasă sau a unui membru sau în timpul unei instanții a șablonului de clasă sau a unui membru. [Exemplu:
typedef double A;
template class B {
typedef int A;
};
template struct X : B {
A a;//a has typedouble
};
The type name A
in the definition of X
binds to the typedef name defined in the global namespace scope, not to the typedef name defined in the base class B
. ] [Example:
struct A {
struct B { /* ... */ };
int a;
int Y;
};
int a;
template struct Y : T {
struct B { /* ... */ };
B b; //The B defined in Y
void f(int i) { a = i; }//::a
Y* p;//Y
};
Y ya;
The members A::B
, A::a
, and A::Y
of the template argument A
do not affect the binding of names in Y
. ]