Return type deduction for class methods? C++1y

别来无恙 提交于 2019-12-04 03:30:18

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/19846097/return-type-deduction-for-class-methods-c1y

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!