간단한 템플릿 기반 모듈 기반 헤더 라이브러리를 작성했습니다. 모듈 기반의 경우, string.h
또는 dynarray.h
만 포함 할 수 있으며 헤더는 모든 종속성을 끌어 당깁니다.모듈 기반 라이브러리에 자체 고정 블로킹 포함
이제이 시스템의 작동 방식 때문에 누락 된 유형의 문제가 있습니다. 모듈을 수행합니다
#include
모든 종속성- 인터페이스
class Foo
#include
에게
불행하게도, 어떤 상황에서, 두 개의 인터페이스를 사용할 수 있어야 구현 파일을 정의 어떤 구현을 포함하여 전 . 여기 문제가 무산 :
string.h를
#pragma once
// A string depends on a DynArray.
#include "dynarray.h"
template<typename E>
class String {
public:
DynArray<E> arr;
/* ... */
};
// Include the implementation of all the different functions (irrelevant here)
#include "string_impl.h"
dynarray.h
#pragma once
// The dynarray header has no direct dependencies
template<typename E>
class DynArray {
public:
/* ... */
E& Get(int index);
};
// Include the implementation of all the different functions
#include "dynarray_impl.h"
dynarray_impl.h
#pragma once
// The dynarray implementation needs the OutOfRangeException class
#include "out_of_range_exception.h"
template<typename E>
E& DynArray<E>::Get(int index) {
if (index >= size) {
throw OutOfRangeException("Some error message");
}
}
class OutOfRangeException {
public:
String message;
OutOfRangeException(String message) {
/* ... */
}
};
out_of_range_exception.h
string.h
을 어딘가에 포함하면 모듈 구현이 포함되므로 dynarray_impl.h
및 out_of_range_exception.h
의 내용이 문자열 클래스 인터페이스 앞에옵니다. 따라서 String
은 OutOfRangeException
에 정의되어 있지 않습니다.
분명히 해결 방법은 문자열 인터페이스의 정의 이후에 dynarray (dynarr_impl.h
)의 구현 부분 만 지연시키는 것입니다. 문제는 모듈 기반 접근 방식과 호환되지 않는 일종의 공통 헤더 파일을 만들지 않고 어떻게해야하는지 잘 모르는 것입니다.