3 つのファイルにまたがる次のコードを考えてみましょう。
// secret.h
#pragma once
class Secret { /* ... */ };
// foo.h
#pragma once
#include "secret.h"
template <typename T>
class Foo {
public:
// Other members ...
void bar();
};
/* Definition is included inside 'foo.h' because Foo is a template class. */
template <typename T> void Foo<T>::bar() {
Secret s;
/* Perform stuff that depend on 'secret.h' ... */
}
// main.cpp
#include "foo.h"
int main() {
Foo<int> foo;
Secret secret; // <--- This shouldn't be allowed, but it is!
return 0;
}
したがって、私の問題は、明示的に使用しない限り、 SecretをFoo のユーザーから隠したいということです。通常、これはinを含めることによって行われます。ただし、Fooはテンプレート クラスであり、その定義を宣言から分離できないため、これは不可能です。明示的なテンプレートのインスタンス化はオプションではありません。#include "secret.h"
secret.h
foo.cpp
最終的に、明示的なテンプレートのインスタンス化以外の方法でこれが可能かどうかを知りたいです。ありがとう!