Functions with deduced return type and class templates

259 views
Skip to first unread message

gonza...@gmail.com

unread,
Aug 13, 2013, 10:41:00 AM8/13/13
to std-dis...@isocpp.org
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

jonatha...@gmail.com

unread,
Aug 14, 2013, 9:26:37 AM8/14/13
to std-dis...@isocpp.org, gonza...@gmail.com


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.

Gonzalo BG

unread,
Aug 14, 2013, 9:33:25 AM8/14/13
to jonatha...@gmail.com, std-dis...@isocpp.org
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.

Ville Voutilainen

unread,
Aug 14, 2013, 10:09:04 AM8/14/13
to std-dis...@isocpp.org, Jonathan Wakely
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.

Richard Smith

unread,
Aug 14, 2013, 5:03:12 PM8/14/13
to std-dis...@isocpp.org, jonatha...@gmail.com
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/.
 
 

David Krauss

unread,
Aug 19, 2013, 11:34:50 PM8/19/13
to std-dis...@isocpp.org, gonza...@gmail.com
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).

Gonzalo BG

unread,
Aug 20, 2013, 3:22:27 AM8/20/13
to David Krauss, std-dis...@isocpp.org
Why can't it be used in a subsequent member signature? They type has already been deduced.

David Krauss

unread,
Aug 21, 2013, 12:48:45 AM8/21/13
to std-dis...@isocpp.org, David Krauss, gonza...@gmail.com
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.
Reply all
Reply to author
Forward
0 new messages