如何优雅地将长类型名称传递给模板参数?
How to elegantly pass long type name to template parameter?
我正在使用 C++17。我有一些代码如下所示:
typedef uint64_t z_hash_t;
struct MoveEdge {
uint64_t dest_hash;
uint32_t times_played;
uint32_t move_key;
std::string pgn_move;
};
struct OpeningTablebase
{
std::unordered_map<z_hash_t, std::shared_ptr<std::vector<MoveEdge>>> m_tablebase;
};
void foo(){
typedef std::__1::unordered_map<z_hash_t, std::__1::shared_ptr<std::__1::vector<MoveEdge>>>::iterator tablebase_iter;
auto root = m_tablebase.find(m_root_hash);
std::queue<tablebase_iter> to_visit;
to_visit.push(root);
}
有没有更好的方法将root
的类型传递给std::queue
的模板参数?使用 typedef
创建别名 tablebase_iter
似乎比在参数中粘贴整个名称要好一些,但我想知道是否有更多 concise/elegant 的方法。
我不确定语法,我希望我能做这样的事情:
std::queue<typeof root> to_visit;
你差不多明白了。您正在寻找 decltype
:
void foo()
{
auto root = m_tablebase.find(m_root_hash);
// VVVVVVVVVVVVV
std::queue<decltype(root)> to_visit;
to_visit.push(root);
}
我正在使用 C++17。我有一些代码如下所示:
typedef uint64_t z_hash_t;
struct MoveEdge {
uint64_t dest_hash;
uint32_t times_played;
uint32_t move_key;
std::string pgn_move;
};
struct OpeningTablebase
{
std::unordered_map<z_hash_t, std::shared_ptr<std::vector<MoveEdge>>> m_tablebase;
};
void foo(){
typedef std::__1::unordered_map<z_hash_t, std::__1::shared_ptr<std::__1::vector<MoveEdge>>>::iterator tablebase_iter;
auto root = m_tablebase.find(m_root_hash);
std::queue<tablebase_iter> to_visit;
to_visit.push(root);
}
有没有更好的方法将root
的类型传递给std::queue
的模板参数?使用 typedef
创建别名 tablebase_iter
似乎比在参数中粘贴整个名称要好一些,但我想知道是否有更多 concise/elegant 的方法。
我不确定语法,我希望我能做这样的事情:
std::queue<typeof root> to_visit;
你差不多明白了。您正在寻找 decltype
:
void foo()
{
auto root = m_tablebase.find(m_root_hash);
// VVVVVVVVVVVVV
std::queue<decltype(root)> to_visit;
to_visit.push(root);
}