【问题标题】:Boost keywords template提升关键字模板
【发布时间】:2022-12-08 22:54:10
【问题描述】:

我的问题是关于这段代码:

 void put(const Tkey& key, const Tval& val){
    auto& lookup = container.template get<1>();
    auto it = lookup.find(key);
    if( it != lookup.end() ) {
      lookup.modify(it,[&](value_type& x){ x.second = val; });
    }
    else{
      it=lookup.emplace(key, val).first;
    }
    container.relocate(container.begin(),container.template project<0>(it));
    capacityOut();
  };

第一名:.template

根据我的发现,我的容器上指定的模板关键字正在使用模板,如果我错了请纠正我。

来源:(Where and why do I have to put the "template" and "typename" keywords?

第二名:project&lt;0&gt;(it)

在库中查找定义,我看到它需要一个迭代器作为参数,但我不明白project&lt;0&gt;(与get&lt;1&gt; 相同)。

我找到了一些像这样的信息:https://theboostcpplibraries.com/boost.variant,以及 Stack Overflow 上的帖子,但我有点困惑。

【问题讨论】:

    标签: c++ templates boost containers boost-multi-index


    【解决方案1】:

    正如here 所解释的,在这两种情况下您都需要template 关键字,因为container::projectcontainer::get 是(大概——我们这里没有代码)模板成员函数。要实例化它们,您需要指定模板参数。但是,正如链接答案中详述的那样,编译器不会理解这样的代码

    container.project<0>(it)
    

    并且需要在您的代码中使用 template 关键字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 2014-05-02
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多