현재 정수로 식별되는 다양한 유형의 개체가있는 매우 오래된 (20 년 이상) 대규모 (15 개 KLOC) 라이브러리를 유지 관리하고 있습니다. 이것은 정수를 주었을 때 어떤 유형의 객체인지 식별 할 수 없다는 문제점을 제기합니다. 이것은 컴파일 타임에 정말 좋을 것입니다.최소한의 변경으로 이전 코드에서 형식 기반 식별 번호 만들기
최소한의 변경으로 해결할 수있는 해결책은 ID 템플릿을 만든 다음 다양한 유형의 개체 ID에 대해 ID 템플릿을 만드는 것입니다.
두 개의 완전히 다른 식별자가 동일한 기본 유형과 범위를 가질 수 있으므로 템플릿에 세 번째 매개 변수를 추가해야한다는 것을 알았습니다.
나는 C++이typedef int X;
typedef int Y;
으로 완전히 다른 유형을 고려하지 않습니다 발견했다. 관리 높은 LOC 무서워의
단순화를 변경 - 합리적인
A) (나는 그것이 작동 알고)
B)이 일을 다른 간단한 방법이 있나요 - :
이 솔루션인가 솔루션을 예로 든다.#include <iostream>
// Horrible old definition of current code
class OldObjectA
{
public:
int ident_; // int identifier
int uniq_; // another int identifier unique to OldObjectA's only
};
class OldObjectB
{
public:
int ident_;
int next_; // int of another OldObjectB ident_
int uniq_; // another int identifier unique to OldObjectB's only
int dq_; // int of yet anothera OldObjectB ident_
int com_; // int of ident_ of a OldObjectA
int ld_; // int of ident_ of a OldObjectC
};
class OldObjectC
{
public:
int indent_;
int next_; // int of another OldObjectC ident_
int com_; // int of ident_ of a OldObjectA
};
enum Type { TypeA, TypeAU, TypeB, TypeBU, TypeC };
template<class T, T maxval, Type type>
class ID
{
public:
friend bool operator==(const ID<T, maxval, type> &lhs, const ID<T, maxval, type> &rhs)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
return true;
}
};
typedef ID<int, 42, TypeA> ID_A;
typedef ID<int, 42, TypeAU> ID_UniqA;
typedef ID<int, 42, TypeB> ID_B;
typedef ID<int, 42, TypeBU> ID_UniqB;
typedef ID<int, 100, TypeC> ID_C;
// What I was thinking of doing
class NewObjectA
{
public:
ID_A ident_; // int identifier
ID_UniqA uniq_; // another int identifer
};
class NewObjectB
{
public:
ID_B ident_;
ID_B next_; // int of another OldObjectB ident_
ID_UniqB uniq_; // another int
ID_B dq_; // int of yet anothera OldObjectB ident_
ID_A com_; // int of ident_ of a OldObjectA
ID_C ld_; // int of ident_ of a OldObjectC
};
class NewObjectC
{
public:
ID_C indent_;
ID_C next_; // int of another OldObjectC ident_
ID_A com_; // int of ident_ of a OldObjectA
};
int main(int argc, char *argv[])
{
std::cout << "================================================================================\n";
ID_A a,a2;
ID_UniqA au,au2;
ID_B b,b2;
ID_UniqB bu,bu2;
ID_C c,c2;
a==a2;
au==au2;
b==b2;
bu==bu2;
c==c2;
// wanted and expected compile time fails
// a=au;
// a=b;
// a=bu;
// a=c;
// au=b;
// au=bu;
// au=c;
// b=bu;
// b=c;
std::cout << "================================================================================\n";
return 0;
}