Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 887
Tim Seguine Avatar asked Nov 07 '13 20:11

Tim Seguine


1 Answers

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.

like image 148
aaronman Avatar answered Sep 29 '22 20:09

aaronman