类方法的返回类型演绎? C ++ 1Y [英] Return type deduction for class methods? C++1y

查看:250
本文介绍了类方法的返回类型演绎? C ++ 1Y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我问,因为我有点含蓄地认为它会工作,但在gcc 4.8.1中,我得到一个内部编译器错误(in gen_type_die_with_usage)。我第一次得到这样一个神秘的错误,所以我有点怀疑;我知道自那以后他们已经改变了规范。



为了清楚起见,这适用于我:

  auto foo(){return 5;} 



  class Bar {
auto baz(){return 5;}
}

标准草案中允许这样做吗?

解决方案

是的标准应该允许它根据文件 n3582 。这是一篇来自论文的例子。
$ b


允许使用自动返回类型的非定义函数声明不是必须的,但它对于更喜欢定义成员函数的编码样式在类之外:



  struct A {
auto f(); //前置声明
};
auto A :: f(){return 42; }




如果我们在这种情况下允许它,它应该是有效的在其他情况下也是如此。允许它也是更正交的选择;总的来说,我相信如果将两个功能结合起来就可以发挥作用。根据@bamboon的评论,返回类型扣除仅在gcc 4.9支持。

所以这可以解释为什么你没有它。


Is return type deduction allowed for member functions in c++14, or only for free functions?

I ask because I sort of implicitly assumed it would work, but in gcc 4.8.1 I get an internal compiler error("in gen_type_die_with_usage"). First time I have ever gotten such a cryptic error like that, so I am a bit skeptical; and I know they have changed the spec since then.

For clarity this works for me:

auto foo() {return 5;}

but this doesn't:

class Bar{
auto baz() {return 5;}
}

Is this allowed in the draft standard?

解决方案

Yes the standard should allow it according to the paper n3582. Here is an example from the paper.

Allowing non-defining function declarations with auto return type is not strictly necessary, but it is useful for coding styles that prefer to define member functions outside the class:

    struct A {
      auto f(); // forward declaration
    };
    auto A::f() { return 42; }

and if we allow it in that situation, it should be valid in other situations as well. Allowing it is also the more orthogonal choice; in general, I believe that if combining two features can work, it should work.

According to the comment by @bamboon, "Return type deduction is only supported as of gcc 4.9." so that would explain why you don't have it.

这篇关于类方法的返回类型演绎? C ++ 1Y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆