我想用C ++创建一个简单的HTML dom构建器,并决定使用模板化的tag<>
类来描述这个标签的类型。
我已经使用其他方法在C ++中创建DOM并取得了一些成功,但是设计不会处理原始字符串,因此移植到模板化类可能会帮助我使用模板特化(tag<plain>
)来处理它。
现在的问题是使用可变参数模板将标记嵌套在其构造函数中。我已经能够用node
实现它,它保存了根级标签,但任何tag-within-tag嵌套都是不行的。
#include <map>
#include <string>
#include <tuple>
#include <utility>
namespace web {
enum class attrs { charset, name, content, http_equiv, rel, href, id, src, lang };
using attribute = std::pair<attrs, std::string>;
using attribute_type = std::map<attrs, std::string>;
const auto none = attribute_type{};
enum tag_name { html, head, meta, title, link, body, div, script, plain, p, h1, span };
template <typename... Tags> struct node {
int increment;
std::tuple<Tags...> tags;
explicit node(const int incr, Tags... tggs)
: increment{incr}, tags{std::make_tuple(tggs...)} {}
};
template <tag_name T, typename... Tags> struct tag {
attribute_type attributes;
std::tuple<Tags...> tags;
explicit tag(attribute_type atts, Tags... tggs)
: attributes{atts.begin(), atts.end()}, tags{std::make_tuple(tggs...)} {
}
};
template <> struct tag<plain> {
std::string content;
explicit tag(std::string val) : content{std::move(val)} {}
};
} // namespace web
int main() {
using namespace web;
node page1{2};
node page2{2, tag<html>{none}};
node page3{2, tag<html>{{{attrs::lang, "en"}}}};
node page4{2, tag<meta>{{{attrs::name, "viewport"},
{attrs::content,
"width=device-width, initial-scale=1.0"}}}};
node page5{2, tag<head>{none}, tag<body>{none}, tag<plain>{"Hello World"}}; // Yet this line still compiles and works as expected...
node page6{1, tag<span>{none, tag<h1>{none}}}; // error: no matching constructor for initialization of 'tag<html>'
}
我想知道我如何能够在tag
类中聚合标签内部的标签,但是如果可能的话,我将能够解决这个问题。
代码中的问题是,在C ++ 17中引入的演绎指南只能推导出所有模板参数。
所以打电话
node page2{2, tag<html>{none}};
因为
(1)tag<html>{none}
不需要模板推导,因为第一个模板参数被说明,其中可变列表(Tags...
)是空的(none
之后没有参数)所以tag
是tag<html>
和
(2)node
的自动扣除指南推导出所有模板参数(Tags...
)所以page2
被推断为node<tag<html>>
。
你写的时候会出现问题
tag<span>{none, tag<h1>{none}}
因为,对于tag<span>
,在none
之后有一个参数,所以变量列表Tags...
不是空的但不能(自动地,通过隐式演绎指南)因为你已经解释了第一个模板参数(span
)。
你可以明显地解决添加make_tag()
函数的问题,正如Cruz Jean所建议的那样,但我建议你使用自动演绎指南的另一种解决方案。
首先,为w
s定义一个包装类tag_name
template <tag_name>
struct w
{ };
然后用两个构造函数重写你的tag
类;第一个用于空内部tags
的情况
explicit tag (attribute_type atts)
: attributes{std::move(atts)}
{ }
第二个一般情况(也不是空的内部tags
列表)接收w<T>
元素,允许自动扣除T
explicit tag (w<T>, attribute_type atts, Tags... tggs)
: attributes{std::move(atts)}, tags{tggs...}
{ }
第一个构造函数允许维护格式
tag<html>{none}
如果没有包含标签;第二个允许这种类型的tag
对象声明
tag{w<html>{}, none}
tag{w<span>{}, none, tag<h1>{none}}
以下是完整的编译示例
#include <map>
#include <string>
#include <tuple>
#include <utility>
namespace web
{
enum class attrs
{ charset, name, content, http_equiv, rel, href, id, src, lang };
using attribute = std::pair<attrs, std::string>;
using attribute_type = std::map<attrs, std::string>;
const auto none = attribute_type{};
enum tag_name
{ html, head, meta, title, link, body, div, script, plain, p, h1, span };
template <typename... Tags>
struct node
{
int increment;
std::tuple<Tags...> tags;
explicit node (int const incr, Tags ... tggs)
: increment{incr}, tags{tggs...}
{ }
};
template <tag_name>
struct w
{ };
template <tag_name T, typename ... Tags>
struct tag
{
attribute_type attributes;
std::tuple<Tags...> tags;
explicit tag (attribute_type atts)
: attributes{std::move(atts)}
{ }
explicit tag (w<T>, attribute_type atts, Tags... tggs)
: attributes{std::move(atts)}, tags{tggs...}
{ }
};
template <>
struct tag<plain>
{
std::string content;
explicit tag (std::string val) : content{std::move(val)}
{ }
};
} // namespace web
int main ()
{
using namespace web;
node page1{2};
node page2{2, tag<html>{none}};
node page3{2, tag<html>{{{attrs::lang, "en"}}}};
node page4{2, tag<html>{{{attrs::name, "viewport"},
{attrs::content, "width=device-width, initial-scale=1.0"}}}};
node page5{2, tag<head>{none}, tag<body>{none},
tag<plain>{"Hello World"}};
node page6{1, tag{w<span>{}, none, tag<h1>{none}}};
}
这似乎是模板类类型推导的问题。有一个歧义可以通过一个简单的函数包装器(或C ++ 17演绎指南)来解决。
无论如何,这里你去(这适用于C ++ 17模式下的gcc 8.3):
#include <map>
#include <string>
#include <tuple>
#include <utility>
namespace web
{
enum class attrs { charset, name, content, http_equiv, rel, href, id, src, lang };
using attribute = std::pair<attrs, std::string>;
using attribute_type = std::map<attrs, std::string>;
const auto none = attribute_type{};
enum tag_name { html, head, meta, title, link, body, div, script, plain, p, h1, span };
template <typename... Tags>
struct node
{
int increment;
std::tuple<Tags...> tags;
explicit node(const int incr, Tags... tggs) : increment{incr}, tags{tggs...} {}
};
template <tag_name T, typename... Tags>
struct tag
{
attribute_type attributes;
std::tuple<Tags...> tags;
explicit tag(const attribute_type &atts, Tags... tggs) : attributes(atts), tags(tggs...) {}
};
template <>
struct tag<plain>
{
std::string content;
explicit tag(std::string val) : content(std::move(val)) {}
};
template<typename ...Args>
auto make_node(int incr, Args &&...args)
{
return node<std::decay_t<Args>...> ( incr, std::forward<Args>(args)... );
}
template<tag_name T, typename ...Args>
auto make_tag(const attribute_type &atts, Args &&...args)
{
return tag<T, std::decay_t<Args>...> ( atts, std::forward<Args>(args)... );
}
} // namespace web
int main() {
using namespace web;
node page1{2};
node page2{2, tag<html>{none}};
node page3{2, tag<html>{{{attrs::lang, "en"}}}};
node page4{2, tag<meta>{{{attrs::name, "viewport"},
{attrs::content,
"width=device-width, initial-scale=1.0"}}}};
node page5{2, tag<head>{none}, tag<body>{none}, tag<plain>{"Hello World"}};
auto page6 = make_node(1, make_tag<span>(none, make_tag<h1>(none))); // works now - uses our make functions
}