有一些函数传入了没有任何作用的变量,但是,这些变量在以后可能会用上(可扩展性)。
而编译器一般会发出警告,提示该变量没有被使用。如何处理这种case呢?
业界有一行统一的代码来解决这个问题。
handler_t stat_cache_handle_fdevent(server *srv, void *_fce, int revent) { size_t i; stat_cache *sc = srv->stat_cache; size_t events; UNUSED(_fce); /* */ if ((revent & FDEVENT_IN) && sc->fam) { events = FAMPending(sc->fam); for (i = 0; i < events; i++) { FAMEvent fe; fam_dir_entry *fam_dir; splay_tree *node; int ndx, j; FAMNextEvent(sc->fam, &fe); /* handle event */ switch(fe.code) { case FAMChanged: case FAMDeleted: case FAMMoved: /* if the filename is a directory remove the entry */ fam_dir = fe.userdata; fam_dir->version++; /* file/dir is still here */ if (fe.code == FAMChanged) break; /* we have 2 versions, follow and no-follow-symlink */ for (j = 0; j < 2; j++) { buffer_copy_string(sc->hash_key, fe.filename); buffer_append_long(sc->hash_key, j); ndx = hashme(sc->hash_key); sc->dirs = splaytree_splay(sc->dirs, ndx); node = sc->dirs; if (node && (node->key == ndx)) { int osize = splaytree_size(sc->dirs); fam_dir_entry_free(node->data); sc->dirs = splaytree_delete(sc->dirs, ndx); assert(osize - 1 == splaytree_size(sc->dirs)); } } break; default: break; } } } if (revent & FDEVENT_HUP) { /* fam closed the connection */ srv->stat_cache->fam_fcce_ndx = -1; fdevent_event_del(srv->ev, &(sc->fam_fcce_ndx), FAMCONNECTION_GETFD(sc->fam)); fdevent_unregister(srv->ev, FAMCONNECTION_GETFD(sc->fam)); FAMClose(sc->fam); free(sc->fam); sc->fam = NULL; } return HANDLER_GO_ON; }其中的:
UNUSED(_fce);
就是用来处理这种情况。
UNUSED宏定义如下:
#define UNUSED(x) ( (void)(x) )
明白了吧?