简体   繁体   English

c ++包括未命名的命名空间中的头文件

[英]c++ including header file in unnamed namespace

I have a class called Cache used within a toolkit (a file with various publicly accessible methods). 我在工具包(具有各种可公开访问的文件的文件)中使用了一个称为Cache的类。 Cache is refreshed using a callback, which requires a functor object. 使用回调刷新缓存,这需要一个仿函数对象。 The functor object calls one of Cache's functions, refresh() on the instance of Cache in my toolkit. functor对象在我的工具包中调用Cache的一个函数,就是Cache的实例上的refresh()。

The instance is within an unnamed namespace in the toolkit (since I don't want clients having direct access to it). 该实例位于工具包中未命名的命名空间内(因为我不希望客户端直接访问它)。

Now, I had this all working, but would really like Cache to have its own header file to make it clear what methods are available on it. 现在,我已经完成了所有工作,但是我真的希望Cache具有自己的头文件,以明确其中可用的方法。

My problem is that I have the following: 我的问题是我有以下几点:

// toolkit.cxx
#include "cache.h"

// Can't define operator()() here since it needs access the to cache instance
struct Functor {
   void operator() ();
};

// Define Cache's fucntions here (including use of Functor)

namespace {
   Cache cache;

// This gives a compiler error - definition of operator Functor::()() is not in namespace enclosing 'Functor'
   void Functor::operator() () {
      cache.refresh();
   }
}

So I can't define Functor::operator()() inside the unnamed namespace, and it can't go outside either. 所以我不能在未命名的命名空间中定义Functor::operator()() ,它也不能在外面。

One solution I have considered is to bring the whole lot inside the unnamed namespace, but this would have to include the #include as well. 我考虑过的一种解决方案是将全部内容引入未命名的名称空间,但这也必须包括#include Is this recommended? 这是推荐的吗? It's not something I have really seen done before (which suggests it may be a bad plan...), and I couldn't find much information on the pros/cons of such an approach. 这不是我以前真正看过的事情(这表明这可能是一个糟糕的计划...),而且我找不到关于这种方法的利弊的太多信息。

This would solution would look like: 这个解决方案看起来像:

// toolkit.cxx

namespace {
   #include "cache.h"

   Cache cache;

   struct Functor {
     void operator()() {
        cache.refresh();
   };

  // Define Cache's fucntions here (including use of Functor)
}

Could anyone comment on the pros/cons (especially cons) of this second approach? 任何人都可以评论第二种方法的利弊吗? Any alternative solutions would also be welcome 任何替代解决方案也将受到欢迎

Solution 1 解决方案1

Define Functor within the anonymous namespace . 在匿名namespace定义Functor

#include "cache.h"

namespace {

   Cache cache;

   struct Functor {
      void operator() () {
         cache.refresh();
      }
   };
}

Solution 2 解决方案2

Define Functor after the definition of the anonymous namespace . 在定义匿名namespace之后定义Functor

#include "cache.h"

namespace {

   Cache cache;

}

struct Functor {
   void operator() () {
      cache.refresh();
   }
};

Solution 3 解决方案3

Declare Functor before the anonymous namespace but define Functor::operator() after the definition of the anonymous namespace . 声明Functor匿名之前namespace ,但是可以定义Functor::operator()匿名的定义后namespace

#include "cache.h"

struct Functor {
   void operator() ();
};

namespace {

   Cache cache;

}

void Functor::operator() () {
   cache.refresh();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM