在编译时阻止标题包含在某些文件中?

Max*_*rin 4 c++ metaprogramming compile-time template-meta-programming

我有一个头文件,我可以控制它的内容.

另外,我有一个接口I1(在一些其他文件中定义),从中派生出各种实现.我想禁止这些实现包含此头文件.因此,在编译期间,如果包含该文件,编译将失败,否则它将照常继续.

所以我有头文件和接口定义(在其他一些文件中).我想在编译期间禁止接口实现包含给定的头文件.

关于如何实现这一点的任何建议?我可以使用一些聪明的模板/预处理技巧吗?

Dav*_*rtz 9

在头文件中:

#ifndef FOO_IMPLEMENTATION_USE_ONLY
#error This file is for inclusion in the FOO implementation only
#endif
Run Code Online (Sandbox Code Playgroud)

在应该包含它的文件中:

// Define this ONLY in the Foo implementation files
#define FOO_IMPLEMENTATION_USE_ONLY
Run Code Online (Sandbox Code Playgroud)