1

我继承了一些名字不好的代码,幸运的是,我收到了一个第三方库,这让我的生活变得更加复杂。这就是我最终的结果。

class Something; // third party library

namespace Something {
  class Something;
  class Templated<class TemplateClass>;
}

现在我需要使用第三方库中的类“Something”作为命名空间Something下的新类的TemplateClass参数。我认为这应该工作

class Something; // third party library

namespace Something {
  class Something;
  class Templated<class TemplateClass>;

  class Impl : public Templated< ::Something > {}
}

但是编译器不喜欢它。我编译它的唯一方法是

class Something; // third party library

class Something2 : public Something {} // dirty hack

namespace Something {
  class Something;
  class Templated<class TemplateClass>;

  class Impl : public Templated< Something2 > {}
}

但我真的不喜欢它。必须有更好的方法来做到这一点。

4

1 回答 1

3

您可以使用另一个命名空间:

class Something; // third party library

namespace third_party{
  using ::Something;
}

namespace Something {
  class Something;
  class Templated<class TemplateClass>;

  class Impl : public Templated< ::third_party::Something > {}
}

不过,总的来说,我认为将您的类和命名空间命名为完全相同是一个非常糟糕的主意。

于 2012-09-25T09:05:51.200 回答