成员函数模板不在clang上编译,但在GCC上编译

时间:2017-01-26 14:34:14

标签: c++ clang

在下面的代码中,我有一个名为zipcode的类模板,它有一个名为get_postbox的成员函数模板。它汇编在海湾合作委员会,但不是在第3.9条。为什么没有接受这个代码呢?

在clang中我收到此错误:

<source>:34:41: error: expected expression
return my_postoffice.get_postbox<K>();
^

此外,从名为zipcode::get_postbox非成员功能模板调用相同的代码(与non_member_function_template()相同)不会导致错误!

要亲自查看并使用它,请参阅编译器资源管理器中的代码:https://godbolt.org/g/MpYzGP

以下是代码:

template <int K>
struct postbox
{
    int val() const 
    {
      return K;
    }
};

template <int A, int B, int C>
struct postoffice
{
  postbox<A> _a;

  template<int I>
  postbox<I> get_postbox()
  {
    switch( I )
    {
      case A: return _a;
    }
  }
};

template <typename PO>
struct zipcode
{
  PO my_postoffice;

  template<int K>
  postbox<K> get_postbox()
  {
    // The error is on this line
    return my_postoffice.get_postbox<K>();
  }
};

// Here's a function template that isn't a member, and it compiles.
template<int D>
int non_member_function_template()
{
  postoffice<123,345,678> po;
  auto box = po.get_postbox<D>();
  return box.val(); 
}

int test_main()
{
  return non_member_function_template<123>();
}

1 个答案:

答案 0 :(得分:1)

使用模板成员函数时需要使用template关键字:

po.template get_postbox<D>()

while (($line = fgets($currentfile)) !== false) {
    $bom = pack('H*','EFBBBF');
    $line = preg_replace("/^$bom/", '', $line);

    $n = sscanf($line, "%d|%d|%[^|]|%[^\n]", $a, $b, $c, $d);
    print "<tr><td>$a</td><td>$b</td><td>$c</td></tr>";
}

c.f。这里:http://ideone.com/W0owY1代码 在这里:Where and why do I have to put the "template" and "typename" keywords?获取何时使用模板关键字

的确切说明
相关问题