简体   繁体   English

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

[英]Performing initialization of templated class using other templated classes in variadic arguments of constructor

I wanted to create a simple HTML dom builder in C++ and decided I would use a templated tag<> class to describe the type of tag this was. 我想用C ++创建一个简单的 HTML dom构建器,并决定使用模板化tag<>类来描述这个标记的类型。

I already used other methods to create the DOM in C++ with some success, but the design wouldn't handle raw strings, so the move to a templated class may assist me in handling that using template specialization ( tag<plain> ). 我已经使用其他方法在C ++中创建DOM并取得了一些成功,但设计不会处理原始字符串,因此转移到模板化类可能会帮助我使用模板特化( tag<plain> )来处理它。

The issue now is nesting the tags within their constructors using a variadic template. 现在的问题是使用可变参数模板将标记嵌套在其构造函数中。 I've been able to implement it with node , which holds the root level tags, but any tag-within-tag nesting is a no-go. 我已经能够使用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>'
}

I want to know how I'm able to aggregate tags inside the node class yet cannot do so inside the tag class and, if possible, I would be able to solve this problem. 我想知道我如何在节点类中聚合标签但在tag类中不能这样做,如果可能的话,我将能够解决这个问题。

This seems to be an issue of template class type deduction. 这似乎是模板类类型推导的问题。 There's an ambiguity that can be cleared up by a simple function wrapper (or by C++17 deduction guides). 有一个歧义可以通过一个简单的函数包装器(或C ++ 17演绎指南)来解决。

Anyhow, here you go (this works in gcc 8.3 in C++17 mode): 无论如何,这里你去(这适用于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
}

The problem in your code is that deduction guides, introduced in C++17, work only deducing all template arguments. 代码中的问题是,在C ++ 17中引入的演绎指南只能推导出所有模板参数。

So calling 所以打电话

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

works because 因为

(1) tag<html>{none} doesn't needs template deduction because the first template parameter is explicated where the variadic list ( Tags... ) is empty (no arguments after none ) so the tag is a tag<html> and (1) tag<html>{none}并不需要模板演绎,因为第一个模板参数是阐明其中的可变参数列表( Tags... )是空的(后无参数none ),所以tagtag<html>

(2) automatic deduction guides for node deduce all template arguments ( Tags... ) so page2 is deduced as node<tag<html>> . (2) node自动扣除指南推导出所有模板参数( Tags... ),因此page2被推导为node<tag<html>>

The problem arises when you write 你写的时候会出现问题

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

because, for the tag<span> , there is an argument after none so the variadic list Tags... isn't empty but can't be (automatically, through implicit deduction guides) because you have explicated the first template argument ( span ). 因为,对于tag<span>有后一个参数none这样的可变参数列表Tags...不是空的,但不能(自动,通过隐扣导游),因为你已经阐明了第一个模板参数( span )。

You can obviously solve the problem adding a make_tag() function, as suggested by Cruz Jean, but I propose you a different solution that uses automatic deduction guides. 你可以明显地解决添加make_tag()函数的问题,正如Cruz Jean所建议的那样,但我建议你使用自动演绎指南的另一种解决方案。

First of all, define a wrapper class w for tag_name s 首先,为tag_name定义包装类w

template <tag_name>
struct w
 { };

then rewrite your tag class with two constructor; 然后用两个构造函数重写你的tag类; the first one for the case with empty internal tags 第一个用于具有空内部tags

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

the second one for the general case (also not empty internal tags list) that receive a w<T> element that permits the automatic deduction also for T 第二个是一般情况(也不是空的内部tags列表),它接收一个w<T>元素,允许自动扣除T

  explicit tag (w<T>, attribute_type atts, Tags... tggs)
     : attributes{std::move(atts)}, tags{tggs...}
  { }

The first constructor permit to maintain the format 第一个构造函数允许维护格式

 tag<html>{none}

in case of absence of contained tags; 如果没有包含标签; the second one permit this type of tag object declarations 第二个允许这种类型的tag对象声明

 tag{w<html>{}, none}

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

The following is a full compiling example 以下是完整的编译示例

#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM