【问题标题】:Prevent header from being included in some files, in compilation time?在编译时防止标头包含在某些文件中?
【发布时间】:2013-09-20 09:09:50
【问题描述】:

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

此外,我还有一个接口 I1(在其他文件中定义),其中各种 派生的实现。我想禁止那些实现包含这个 头文件。这样在编译期间,如果包含文件,编译将失败, 否则它将照常进行。

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

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

【问题讨论】:

    标签: c++ metaprogramming template-meta-programming compile-time


    【解决方案1】:

    在头文件中:

    #ifndef FOO_IMPLEMENTATION_USE_ONLY
    #error This file is for inclusion in the FOO implementation only
    #endif
    

    在应该包含它的文件中:

    // Define this ONLY in the Foo implementation files
    #define FOO_IMPLEMENTATION_USE_ONLY
    

    【讨论】:

    • 谢谢!看起来它应该做的伎俩!虽然我会使用否定的:#ifdef somethingFromHeader #error....
    猜你喜欢
    • 2013-09-21
    • 1970-01-01
    • 2019-06-28
    • 2012-05-15
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多