成员函数特化中的循环依赖...... "instantiation before specialization"
circular dependencies in member function specializations ....... "instantiation before specialization"
下面的代码无法编译
#include <iostream>
using namespace std;
template<class T> class A;
template<class T> class B;
//@@@@@@@@@@@@@y
template<class T>
class B{
int x=1;
public:
void write(A<T> x);
void add(A<T> x);
};
template<class T>
class A{
int x=1;
public:
void write(B<T> x);
void add(B<T> x);
};
template<class T> void A<T>::write(B<T> x){cout<<"write generic A"<<endl;x.add(*this);};
template<class T> void A<T>::add(B<T> x){cout<<"add generic A"<<endl;};
template<> void A<int>::write(B<int> x){cout<<"write special A"<<endl;x.add(*this);};
template<> void A<int>::add(B<int> x){cout<<"add special A int"<<endl;};
template<class T> void B<T>::write(A<T> x){cout<<"write generic B"<<endl;x.add(*this);};
template<class T> void B<T>::add(A<T> x){cout<<"add generic B"<<endl;};
template<> void B<int>::write(A<int> x){cout<<"write special B"<<endl;x.add(*this);};
template<> void B<int>::add(A<int> x){cout<<"add special B"<<endl;};
int main(){
B<int> b;
A<int> a;
b.write(a);
}
作为A和B的专门“write”方法,实例化A或B的“add”模板。编译编译时抱怨
error: specialization of ‘void B<T>::add(A<T>) [with T = int]’ after instantiation
32 | template<> void B<int>::add(A<int> x){cout<<"add special B"<<endl;};
目前我看不出如何对函数进行排序来避免这个问题,除非我放弃专业化。
非常感谢任何建议。
您可以在每个 class 声明后声明模板方法的特化:
#include <iostream>
using namespace std;
template<class T> class A;
template<class T> class B;
//@@@@@@@@@@@@@y
template<class T>
class B{
int x=1;
public:
void write(A<T> x);
void add(A<T> x);
};
// declare specializations:
template<> void B<int>::write(A<int> x);
template<> void B<int>::add(A<int> x);
template<class T>
class A{
int x=1;
public:
void write(B<T> x);
void add(B<T> x);
};
// declare specializations:
template<> void A<int>::write(B<int> x);
template<> void A<int>::add(B<int> x);
template<class T> void A<T>::write(B<T> x){cout<<"write generic A"<<endl;x.add(*this);};
template<class T> void A<T>::add(B<T> x){cout<<"add generic A"<<endl;};
template<> void A<int>::write(B<int> x){cout<<"write special A"<<endl;x.add(*this);}; // method "B<int>::add(A<int> x)" is called but was not declared or defined before
template<> void A<int>::add(B<int> x){cout<<"add special A int"<<endl;};
template<class T> void B<T>::write(A<T> x){cout<<"write generic B"<<endl;x.add(*this);};
template<class T> void B<T>::add(A<T> x){cout<<"add generic B"<<endl;};
template<> void B<int>::write(A<int> x){cout<<"write special B"<<endl;x.add(*this);};
template<> void B<int>::add(A<int> x){cout<<"add special B"<<endl;};
int main(){
B<int> b;
A<int> a;
b.write(a);
}
代码中有关于方法 B<int>::add(A<int> x)
在 A<int>::write(B<int> x)
中调用但之前未声明或定义的注释。
下面的代码无法编译
#include <iostream>
using namespace std;
template<class T> class A;
template<class T> class B;
//@@@@@@@@@@@@@y
template<class T>
class B{
int x=1;
public:
void write(A<T> x);
void add(A<T> x);
};
template<class T>
class A{
int x=1;
public:
void write(B<T> x);
void add(B<T> x);
};
template<class T> void A<T>::write(B<T> x){cout<<"write generic A"<<endl;x.add(*this);};
template<class T> void A<T>::add(B<T> x){cout<<"add generic A"<<endl;};
template<> void A<int>::write(B<int> x){cout<<"write special A"<<endl;x.add(*this);};
template<> void A<int>::add(B<int> x){cout<<"add special A int"<<endl;};
template<class T> void B<T>::write(A<T> x){cout<<"write generic B"<<endl;x.add(*this);};
template<class T> void B<T>::add(A<T> x){cout<<"add generic B"<<endl;};
template<> void B<int>::write(A<int> x){cout<<"write special B"<<endl;x.add(*this);};
template<> void B<int>::add(A<int> x){cout<<"add special B"<<endl;};
int main(){
B<int> b;
A<int> a;
b.write(a);
}
作为A和B的专门“write”方法,实例化A或B的“add”模板。编译编译时抱怨
error: specialization of ‘void B<T>::add(A<T>) [with T = int]’ after instantiation
32 | template<> void B<int>::add(A<int> x){cout<<"add special B"<<endl;};
目前我看不出如何对函数进行排序来避免这个问题,除非我放弃专业化。
非常感谢任何建议。
您可以在每个 class 声明后声明模板方法的特化:
#include <iostream>
using namespace std;
template<class T> class A;
template<class T> class B;
//@@@@@@@@@@@@@y
template<class T>
class B{
int x=1;
public:
void write(A<T> x);
void add(A<T> x);
};
// declare specializations:
template<> void B<int>::write(A<int> x);
template<> void B<int>::add(A<int> x);
template<class T>
class A{
int x=1;
public:
void write(B<T> x);
void add(B<T> x);
};
// declare specializations:
template<> void A<int>::write(B<int> x);
template<> void A<int>::add(B<int> x);
template<class T> void A<T>::write(B<T> x){cout<<"write generic A"<<endl;x.add(*this);};
template<class T> void A<T>::add(B<T> x){cout<<"add generic A"<<endl;};
template<> void A<int>::write(B<int> x){cout<<"write special A"<<endl;x.add(*this);}; // method "B<int>::add(A<int> x)" is called but was not declared or defined before
template<> void A<int>::add(B<int> x){cout<<"add special A int"<<endl;};
template<class T> void B<T>::write(A<T> x){cout<<"write generic B"<<endl;x.add(*this);};
template<class T> void B<T>::add(A<T> x){cout<<"add generic B"<<endl;};
template<> void B<int>::write(A<int> x){cout<<"write special B"<<endl;x.add(*this);};
template<> void B<int>::add(A<int> x){cout<<"add special B"<<endl;};
int main(){
B<int> b;
A<int> a;
b.write(a);
}
代码中有关于方法 B<int>::add(A<int> x)
在 A<int>::write(B<int> x)
中调用但之前未声明或定义的注释。