簡體   English   中英

在構造函數的可變參數中使用其他模板化類來執行模板化類的初始化

[英]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 ),所以tagtag<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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM