在构造函数的可变参数中使用其他模板化 classes 执行模板化 class 的初始化
Performing initialization of templated class using other templated classes in variadic arguments of constructor
我想在 C++ 中创建一个 简单 HTML dom 构建器并决定使用模板化 tag<>
class 来描述标签的类型。
我已经使用其他方法在 C++ 中创建 DOM 并取得了一些成功,但设计无法处理原始字符串,因此转移到模板化 class 可能会帮助我处理使用模板专业化 (tag<plain>
).
现在的问题是使用可变参数模板将标签嵌套在它们的构造函数中。我已经能够使用 node
来实现它,它包含根级标签,但是任何标签内嵌套都是不行的。
#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>'
}
我想知道我如何能够在节点 class 内聚合标签,但在 tag
class 内无法这样做,如果可能的话,我将能够来解决这个问题。
这似乎是模板 class 类型推导的问题。可以通过简单的函数包装器(或通过 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
}
您的代码中的问题是在 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
).
您显然可以按照 Cruz Jean 的建议添加一个 make_tag()
函数来解决问题,但我建议您使用自动推导指南的不同解决方案。
首先,为tag_name
s
定义一个包装器class w
template <tag_name>
struct w
{ };
然后用两个构造器重写你的tag
class;第一个用于内部空 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++ 中创建一个 简单 HTML dom 构建器并决定使用模板化 tag<>
class 来描述标签的类型。
我已经使用其他方法在 C++ 中创建 DOM 并取得了一些成功,但设计无法处理原始字符串,因此转移到模板化 class 可能会帮助我处理使用模板专业化 (tag<plain>
).
现在的问题是使用可变参数模板将标签嵌套在它们的构造函数中。我已经能够使用 node
来实现它,它包含根级标签,但是任何标签内嵌套都是不行的。
#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>'
}
我想知道我如何能够在节点 class 内聚合标签,但在 tag
class 内无法这样做,如果可能的话,我将能够来解决这个问题。
这似乎是模板 class 类型推导的问题。可以通过简单的函数包装器(或通过 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
}
您的代码中的问题是在 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
).
您显然可以按照 Cruz Jean 的建议添加一个 make_tag()
函数来解决问题,但我建议您使用自动推导指南的不同解决方案。
首先,为tag_name
s
w
template <tag_name>
struct w
{ };
然后用两个构造器重写你的tag
class;第一个用于内部空 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}}};
}