【问题标题】:How to have same header definition in two different namespaces (but written once), with different implementations for namespaces?如何在两个不同的命名空间中具有相同的标头定义(但只编写一次),并具有不同的命名空间实现?
【发布时间】:2019-09-26 23:38:22
【问题描述】:

假设我的程序由 trusteduntrusted 两个组件组成。我只想在两个不同的命名空间中对 A 类的声明进行一次编码,但它们的实现可以根据命名空间而有所不同,只需对公共 API 编码一次。我不想将宏用于#ifdef UNTRSUTED .etc 之类的实现。

我不想使用抽象和继承来实现不同的行为。我只是好奇这是否可能。

在标题 A.h 我将有

// A.h
#pragma once
namespace app {
 // I know I can't get what I want with naming the same namespace twice
 namespace untrusted, trusted {
  class A {
    doDifferentFoo();
    doCommonBar() // this one is common between two impls;
  }
 }
}

在实现中,我将拥有 A-common.cpp(只为两个命名空间实现公共接口一次)、A-untrusted.cpp(为不受信任的命名空间实现 doDifferentFoo)和 A-trusted.cpp(实现doDifferentFoo 用于受信任的命名空间)

【问题讨论】:

  • doDifferentFoo 的两种实现有何不同?条件编译可以工作吗?或者你能提供一个共同的基类,两个类都继承自来处理共同的东西?

标签: c++ namespaces


【解决方案1】:

我想最简单的方法是将通用声明移动到一个额外的文件中,然后将其包含两次:

A_detail.h:

// No `#pragma once` here!
class A {
    doDifferentFoo();
    doCommonBar(); // this one is common between two impls;
};

啊哈:

#pragma once
namespace app {
    namespace trusted {
        #include "a_detail.h"
    }
    namespace untrusted {
        #include "a_detail.h"
    }
}

A-untrusted.cpp:

#include "a.h"
namespace app { namespace untrusted {
    // ...
} }

A-trusted.cpp:

#include "a.h"
namespace app { namespace trusted {
    // ...
} }

A-common_detail.cpp(可能选择不同的文件结尾;不应编译为翻译单元):

// common definitions/declarations without `namespace`

A-common.cpp:

namespace app {
    namespace untrusted {
        #include "A-common_detail.cpp"
    }
    namespace trusted {
        #include "A-common_detail.cpp"
    }
}

我不确定这是否值得。或者,您可以(在每个具有公共代码的文件中)对所有公共代码使用宏,并为两个命名空间调用两次。但是你确实说过你不想使用宏。

没有预处理器的帮助就无法做到这一点,因为每个声明(只有一个声明器)在一个范围内声明了一个名称。

【讨论】:

    【解决方案2】:

    //啊.h

    class A {
        void doDifferentFoo();
    
        void doCommonBar()
        { // ...
        }
    
    };
    

    // A_trusted.h

    namespace app
    {
    namespace trusted
    {
    #include "A.h"
    
    void A::doDifferentFoo() // can be moved to cpp-file if needed/wanted
    {
    }
    
    }
    }
    

    // A_untrusted.h

    namespace app
    {
    namespace untrusted
    {
    #include "A.h"
    
    void A::doDifferentFoo() // can be moved to cpp-file if needed/wanted
    {
    }
    
    }        
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-10
      • 2020-07-13
      • 2014-09-27
      • 2010-11-12
      • 2014-01-12
      • 1970-01-01
      • 2011-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多