Can friend class be declared conditionally in C++03?

前端 未结 5 1396
半阙折子戏
半阙折子戏 2021-01-19 02:54

I want to declare a friend class only if some (compile-time) condition is true. For example:

// pseudo-C++
class Foo {
    if(some_compile_time_condition) {
         


        
5条回答
  •  广开言路
    2021-01-19 03:35

    It seems, unfortunately, not possible within the C++ compiler: ie, it seems that only the preprocessor may help here. Note: Johannes has a proposal, so there is hope!

    However I would note that:

    • friendship does not require you to actually use it
    • friendship is a pure compile-time construct (like access specifiers) and does not incur any runtime penalty on any major compiler

    there is no reason not to have unconditional friendship, but only use it if some conditions (static or dynamic) are met.

    Note: in the future, this is something that the static_if proposal could cover.

提交回复
热议问题