c++ - 在构造函数的可变参数中使用其他模板化类执行模板化类的初始化

标签 c++ constructor c++17 variadic-templates template-argument-deduction

我想用 C++ 创建一个简单 HTML dom 构建器,并决定使用模板化的 tag<>类来描述标签的类型。

我已经使用其他方法在 C++ 中创建 DOM 并取得了一些成功,但该设计无法处理原始字符串,因此转移到模板化类可能有助于我使用模板特化 (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>'
}

我想知道如何在节点类中聚合标签,但在 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++ - 在构造函数的可变参数中使用其他模板化类执行模板化类的初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55714503/

相关文章:

c++ - 无法使用 std::variant 的重载运算符 <<() 流式传输 std::endl

c++ - 输出流作为类成员

c++ - 检查 COM 指针是否存在

c++ - 为什么非可变 lambda 表达式中的字段在捕获常量值或常量引用时使用 "const"?

c++ - 在 C++ 中,只有默认参数的构造函数是默认构造函数吗?

c++ - 函数返回初始化的构造函数

c++ - 如何从连接的控制台执行命令

c++ - 已保存文件的 Windows 事件 Hook

javascript - 定义构造函数的不同方式

c++ - 没有尖括号的模板类实例化