【问题标题】:Can I somehow not write out the full qualified return type name?我可以以某种方式不写出完全限定的返回类型名称吗?
【发布时间】:2016-11-14 01:10:05
【问题描述】:

我有以下嵌套类的情况:

class PS_OcTree {
public:
  // stuff ...

private:
  struct subdiv_criteria : public octree_type::subdiv_criteria {
    PS_OcTree* tree;
    subdiv_criteria(PS_OcTree* _tree) : tree(_tree) { }
    virtual Element elementInfo(unsigned int const& elem, node const* n) override;
  };
};

为了在.cpp文件中实现这个方法,我写

PS_OcTree::subdiv_criteria::Element
PS_OcTree::subdiv_criteria::elementInfo(
  unsigned int const& poly_index, node const* n)
{
    // implementation goes here
}

我可以写方法的全名,但我真的还需要写返回类型的全名吗?在参数括号和函数体中,我可以访问 subdiv_criteria 类的名称,但这似乎不适用于返回类型。

最好写一些类似的东西

Element PS_OcTree::subdiv_criteria::elementInfo(
  unsigned int const& poly_index, node const* n)
{
    // implementation goes here
}

// or

auto PS_OcTree::subdiv_criteria::elementInfo(
  unsigned int const& poly_index, node const* n)
{
    // implementation goes here
}

至少不需要我在返回类型中重复PS_OcTree::subdiv_criteria。我可以使用 C++11 中的某些东西吗?它也应该适用于 MSVC 2015 和 Clang 5。

【问题讨论】:

  • 在 C++11 中,您可以编写 auto PS_OcTree::subdiv_criteria::elementInfo( unsigned int const& poly_index, node const* n) -> Element 。如果 MSVC 支持,则 IDK

标签: c++ c++11 visual-c++ clang code-duplication


【解决方案1】:

类范围查找适用于 declarator-id (这是定义的函数的名称,即PS_OcTree::subdiv_criteria::elementInfo)之后的任何内容,包括尾随返回类型。因此,

auto PS_OcTree::subdiv_criteria::elementInfo(
  unsigned int const& poly_index, node const* n) -> Element 
{
}

【讨论】:

  • 光滑!谢谢,我没有弄清楚这个。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 2012-06-14
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多