通常我们会使用标准类型作为std::unordered_map<key, value>
的键和值。但现在我需要自定义我自己的键和值类。
密钥类在block_cache_key.h
中定义如下:
#ifndef BLOCK_CACHE_KEY_H_
#define BLOCK_CACHE_KEY_H_
#include <functional>
namespace wcg{
class BlockCacheKey{
public:
BlockCacheKey(const std::string &name, int64_t offset) : name_(name), offset_(offset) {}
bool operator==(const BlockCacheKey &other) const{
return offset_ == other.offset_ && name_ == other.name_;
}
const std::string &name() const{
return name_;
}
const int64_t offset() const{
return offset_;
}
std::string to_string() const{
return name_ + "_" + std::to_string(offset_);
}
private:
std::string name_;
int64_t offset_;
};
}
namespace std{
template <>
class hash<wcg::BlockCacheKey>{
public:
size_t operator()(const wcg::BlockCacheKey &key) const{
return std::hash<std::string>()(key.name()) ^ (std::hash<int64_t>()(key.offset()));
}
};
}
#endif
值类在block.h
中定义如下:
#ifndef BLOCK_H_
#define BLOCK_H_
namespace wcg{
class Block{
public:
Block() {}
};
}
#endif
使用std::unorded_map
的类在block_cache.h
中定义如下:
#ifndef BLOCK_CACHE_H_
#define BLOCK_CACHE_H_
#include <iostream>
#include <memory>
#include <unordered_map>
namespace wcg{
class BlockCacheKey;
class Block;
class BlockCache{
public:
BlockCache() : count_(0) {
std::cout << "initial stats: " << to_string() << std::endl;
}
~BlockCache(){
std::cout << "final stats: " << to_string() << std::endl;
}
void CacheBlock(const BlockCacheKey &key, std::shared_ptr<Block> block){
std::cout << "cache put: " << key.to_string() << std::endl;
map_[key] = block;
count_++;
}
std::shared_ptr<Block> GetBlock(const BlockCacheKey &key){
auto pos = map_.find(key);
if(pos != map_.end()){
std::cout << "cache get: " << key.to_string() << std::endl;
return pos->second;
}
return nullptr;
}
std::string to_string() const{
return "block count: " + std::to_string(count_);
}
private:
std::unordered_map<BlockCacheKey, std::shared_ptr<Block>> map_;
int count_;
};
}
#endif
主要功能在main.cpp
中定义如下:
#include <memory>
#include "block_cache_key.h"
#include "block.h"
#include "block_cache.h"
// g++ -std=c++11 -g -Wall main.cpp
int main(int argc, char* argv[]){
wcg::BlockCache bc;
return 0;
}
当#include "block_cache.h"
终于出现时,编译就可以了。但是当#include "block_cache.h"
在#include "block_cache_key.h"
和#include "block.h"
之前时,编译将失败并且错误信息非常难看,很难理解。
我所知道的,std::unordered_map
是一个模板。当GCC编译时,它将首先预处理包含部分,即展开它们。这让我觉得包含订单不是一个问题。
最后要注意的是,我不想在block_cache_key.h
中包含block.h
和block_cache.h
部分编译错误消息:(整个错误消息在https://paste.ubuntu.com/p/VyrWKCTG4q/中)
In file included from /usr/include/c++/7/bits/hashtable.h:35:0,
from /usr/include/c++/7/unordered_map:47,
from block_cache.h:13,
from main.cpp:5:
/usr/include/c++/7/bits/hashtable_policy.h: In instantiation of 'struct std::__detail::__is_noexcept_hash<wcg::BlockCacheKey, std::hash<wcg::BlockCacheKey> >':
/usr/include/c++/7/type_traits:143:12: required from 'struct std::__and_<std::__is_fast_hash<std::hash<wcg::BlockCacheKey> >, std::__detail::__is_noexcept_hash<wcg::BlockCacheKey, std::hash<wcg::BlockCacheKey> > >'
/usr/include/c++/7/type_traits:154:31: required from 'struct std::__not_<std::__and_<std::__is_fast_hash<std::hash<wcg::BlockCacheKey> >, std::__detail::__is_noexcept_hash<wcg::BlockCacheKey, std::hash<wcg::BlockCacheKey> > > >'
/usr/include/c++/7/bits/unordered_map.h:103:66: required from 'class std::unordered_map<wcg::BlockCacheKey, std::shared_ptr<wcg::Block> >'
block_cache.h:50:60: required from here
/usr/include/c++/7/bits/hashtable_policy.h:87:34: error: no match for call to '(const std::hash<wcg::BlockCacheKey>) (const wcg::BlockCacheKey&)'
noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/move.h:54:0,
from /usr/include/c++/7/bits/stl_pair.h:59,
from /usr/include/c++/7/bits/stl_algobase.h:64,
from /usr/include/c++/7/memory:62,
from main.cpp:3:
/usr/include/c++/7/type_traits: In instantiation of 'struct std::__not_<std::__and_<std::__is_fast_hash<std::hash<wcg::BlockCacheKey> >, std::__detail::__is_noexcept_hash<wcg::BlockCacheKey, std::hash<wcg::BlockCacheKey> > > >':
/usr/include/c++/7/bits/unordered_map.h:103:66: required from 'class std::unordered_map<wcg::BlockCacheKey, std::shared_ptr<wcg::Block> >'
block_cache.h:50:60: required from here
/usr/include/c++/7/type_traits:154:31: error: 'value' is not a member of 'std::__and_<std::__is_fast_hash<std::hash<wcg::BlockCacheKey> >, std::__detail::__is_noexcept_hash<wcg::BlockCacheKey, std::hash<wcg::BlockCacheKey> > >'
: public __bool_constant<!bool(_Pp::value)>
^~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/unordered_map:48:0,
from block_cache.h:13,
from main.cpp:5:
/usr/include/c++/7/bits/unordered_map.h: In instantiation of 'class std::unordered_map<wcg::BlockCacheKey, std::shared_ptr<wcg::Block> >':
block_cache.h:50:60: required from here
/usr/include/c++/7/bits/unordered_map.h:103:66: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<std::hash<wcg::BlockCacheKey> >, std::__detail::__is_noexcept_hash<wcg::BlockCacheKey, std::hash<wcg::BlockCacheKey> > > >'
typedef __umap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc> _Hashtable;
^~~~~~~~~~
/usr/include/c++/7/bits/unordered_map.h:110:45: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<std::hash<wcg::BlockCacheKey> >, std::__detail::__is_noexcept_hash<wcg::BlockCacheKey, std::hash<wcg::BlockCacheKey> > > >'
typedef typename _Hashtable::key_type key_type;
我们来看看BlockCache.h
。你向编译器保证Block
和BlockCacheKey
存在并最终定义 - 此时它们是不完整的类型。这是好的并且只要你不以一种要求它们完整的方式使用它们就可以正常工作:将参考,指针甚至std::shared_ptr
s形成为不完整类型都可以。有关您不能使用它们的事项列表,请参阅here。
你在BlockCacheKey
中使用Block
(和std::unordered_map<BlockCacheKey, std::shared_ptr<Block>>
)来实例化std::unordered_map<Key, Value>
模板(这是声明该类型成员所必需的),你必须完成Key
和Value
类型。
想象一下,sizeof(std::unordered_map<Key, Value>)
依赖于sizeof(Key)
(这将属于其权利范围内)。只有Key
的前向声明,sizeof(Key)
将是未知的,因此sizeof(std::unordered_map<Key, Value>)
将是未知的,因此你的BlockCache
的大小将是未知的(即使你定义它!)。编译器不能使用它,这就是你不允许这样做的原因。
最后要注意的是,我不想在block_cache.h中包含block_cache_key.h和block.h
这是不可能的(如您所观察到的,用户不需要时髦的包含订单)。要实例化std::map<BlockCacheKey, Whatever>
,必须知道BlockCacheKey
的定义,你只能从中包含它的标题。我相信对于你打算用作地图值类型的Block
类型的std::shared_ptr<Block>
也是如此。