【发布时间】:2023-10-21 02:41:02
【问题描述】:
所以我试图了解 Boost 的 ptree 实现发生了什么。
在ptree.hpp中basic_ptree实际上是定义的:
template<class Key, class Data, class KeyCompare>
class basic_ptree
在 ptree_fwd.hpp 中有一个看起来像 basic_ptree 的前向声明,但带有一个新的模板参数默认值:
template < class Key, class Data, class KeyCompare = std::less<Key> >
class basic_ptree;
最后在 ptree_fwd.hpp ptree 是 typedef'd:
typedef basic_ptree<std::string, std::string> ptree;
这是 ptree_fwd.hpp 中的前向声明,对吗?所以我可以在前向声明中默认模板参数吗?
【问题讨论】:
-
您可以在模板声明中为模板参数声明默认值,就像在函数声明中为函数参数声明默认值一样。
-
@SamVarshavchik 是的,知道这一点。我可以在前向声明中默认模板参数吗?
-
这就是我写的。
-
@JonathanMee 声明(可能多次)和定义都可以,最后合并;与函数参数相同。
-
@JonathanMee 我正在写一个答案,一会儿就会清楚:)
标签: c++ templates metaprogramming forward-declaration default-arguments