Functions with deduced return type and class templates

Showing 1-8 of 8 messages
Functions with deduced return type and class templates Gonzalo BG 8/13/13 7:41 AM
I just read the proposal on return type deduction [1] and it is unclear to me 
if the following code would be valid: 

#include <iostream>

template<class T> struct B {
  auto hello() { return std::string{"Hello, I'm a B"}; }
  void print_me() { std::cout << hello() << std::endl; }
};

int main() {
  B<int> b;
  b.print_me();
  return 0;
}

In particular, I wasn't able to find an example there about their usage within 
class templates. Would this be valid code?

Bests,
Gonzalo

Re: Functions with deduced return type and class templates Jonathan Wakely 8/14/13 6:26 AM


On Tuesday, August 13, 2013 3:41:00 PM UTC+1, gonza...@gmail.com wrote:
I just read the proposal on return type deduction [1] and it is unclear to me 
if the following code would be valid: 

#include <iostream>

template<class T> struct B {
  auto hello() { return std::string{"Hello, I'm a B"}; }
  void print_me() { std::cout << hello() << std::endl; }
};

int main() {
  B<int> b;
  b.print_me();
  return 0;
}

In particular, I wasn't able to find an example there about their usage within 
class templates. Would this be valid code?


I don't see why it wouldn't be valid (although the version of G++ 4.9 I tried it with crashed!)

If a valid code construct doesn't work inside templates then that needs to be stated explicitly, so if the proposal doesn't say it doesn't work in templates then assume it does.

Re: Functions with deduced return type and class templates Gonzalo BG 8/14/13 6:33 AM
Clang also fails and reports: error: function 'hello' with deduced return type cannot be used before it is defined. I'll fill a bug report.
Re: [std-discussion] Re: Functions with deduced return type and class templates Ville Voutilainen 8/14/13 7:09 AM



On 14 August 2013 16:33, Gonzalo BG <gonza...@gmail.com> wrote:
Clang also fails and reports: error: function 'hello' with deduced return type cannot be used before it is defined. I'll fill a bug report.

Works on my gcc 4.9 snapshot. For all intents the code looks valid to me, as in it _should_ be valid,
if anyone finds standard wording contradictory, we need a defect report. If the standardese is ok,
it's a matter of reporting implementation bugs.
Re: [std-discussion] Re: Functions with deduced return type and class templates Richard Smith 8/14/13 2:03 PM
On Wed, Aug 14, 2013 at 6:33 AM, Gonzalo BG <gonza...@gmail.com> wrote:
Clang also fails and reports: error: function 'hello' with deduced return type cannot be used before it is defined. I'll fill a bug report.

You're too late, I've already filed this (llvm.org/PR16875) and fixed it. :-)
 
On Wed, Aug 14, 2013 at 3:26 PM, <jonatha...@gmail.com> wrote:


On Tuesday, August 13, 2013 3:41:00 PM UTC+1, gonza...@gmail.com wrote:
I just read the proposal on return type deduction [1] and it is unclear to me 
if the following code would be valid: 

#include <iostream>

template<class T> struct B {
  auto hello() { return std::string{"Hello, I'm a B"}; }
  void print_me() { std::cout << hello() << std::endl; }
};

int main() {
  B<int> b;
  b.print_me();
  return 0;
}

In particular, I wasn't able to find an example there about their usage within 
class templates. Would this be valid code?


I don't see why it wouldn't be valid (although the version of G++ 4.9 I tried it with crashed!)

If a valid code construct doesn't work inside templates then that needs to be stated explicitly, so if the proposal doesn't say it doesn't work in templates then assume it does.


--
 
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-discussion/.
 
 

Re: Functions with deduced return type and class templates David Krauss 8/19/13 8:34 PM
For those following at home (and the OP): The return type is resolved when the function is defined, as all member function definitions are processed in sequence, at the closing brace of the class {} scope.

So a function of deduced return type can be used in a subsequent member function body, but not a preceding member function body, nor a subsequent member signature (such as inside a decltype-specifier).
Re: Functions with deduced return type and class templates Gonzalo BG 8/20/13 12:22 AM
Why can't it be used in a subsequent member signature? They type has already been deduced.
Re: Functions with deduced return type and class templates David Krauss 8/20/13 9:48 PM
On Tuesday, August 20, 2013 3:22:27 PM UTC+8, Gonzalo BG wrote:
Why can't it be used in a subsequent member signature? They type has already been deduced.

The type is deduced from the definition, and such are processed in the scope containing all member declarations, at the closing brace. Between the declaration and the definition, the return type is merely a placeholder.

One auto type can depend on another, but a non-auto type cannot depend on an auto via decltype().

By the way, my previous comment did not address templating. A non-template member function of a class template is instantiated separately, but use in a signature would illegally require instantiation before the definition is visible. Use of a deduced function as an argument-dependent call in a member function template signature might work; I don't see why not but didn't review the paper just now.

In my limited experimentation, it's pretty easy to crash current implementations, regardless of what works in theory.