[英]Performing initialization of templated class using other templated classes in variadic arguments of constructor
我想用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演绎指南)来解决。
无论如何,这里你去(这适用于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
)。
你可以明显地解决添加make_tag()
函数的问题,正如Cruz Jean所建议的那样,但我建议你使用自动演绎指南的另一种解决方案。
首先,为tag_name
定义包装类w
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}}};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.