【问题标题】:Performing initialization of templated class using other templated classes in variadic arguments of constructor在构造函数的可变参数中使用其他模板类执行模板类的初始化
【发布时间】:2019-04-16 18:28:39
【问题描述】:

我想用 C++ 创建一个简单 HTML dom builder,并决定使用模板化的 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 类中这样做,如果可能的话,我将能够解决这个问题。

【问题讨论】:

  • 你真的想计算 html 的时间视图吗?因为您将无法读取文件内容(即“运行时”)。
  • node page6{1, tag&lt;span, tag&lt;h1&gt;&gt;{none, tag&lt;h1&gt;{none}}}; ? Demo

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


【解决方案1】:

这似乎是模板类类型推导的问题。可以通过简单的函数包装器(或通过 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
}

【讨论】:

    【解决方案2】:

    您的代码中的问题是 C++17 中引入的推导指南只能推导 所有 模板参数。

    这样呼唤

    node page2{2, tag<html>{none}};
    

    之所以有效,是因为

    (1) tag&lt;html&gt;{none} 不需要模板推导,因为第一个模板参数在可变参数列表 (Tags...) 为空(none 之后没有参数)的地方进行了说明,因此 tag 是 @987654326 @和

    (2) node 的自动推导指南推导 所有 模板参数 (Tags...),因此 page2 推导为 node&lt;tag&lt;html&gt;&gt;

    问题出现在你写的时候

    tag<span>{none, tag<h1>{none}}
    

    因为对于tag&lt;span&gt;,在none 之后有一个参数,所以可变参数列表Tags... 不为空但不能为空(通过隐式推导指南自动),因为您已经解释了第一个模板论据(span)。

    您显然可以按照 Cruz Jean 的建议添加 make_tag() 函数来解决问题,但我建议您使用自动推导指南的不同解决方案。

    首先,为tag_names定义一个包装类w

    template <tag_name>
    struct w
     { };
    

    然后用两个构造函数重写你的tag类;第一个为空的内部tags

      explicit tag (attribute_type atts)
         : attributes{std::move(atts)}
       { }
    

    第二个用于一般情况(也不是空的内部tags 列表),它接收w&lt;T&gt; 元素,该元素也允许自动扣除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}}};
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 2018-11-23
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多