Return-type deduction is a C++11 feature for lambda expressions and trailing return-types, that also applies to normal functions in C++14.
Questions tagged [return-type-deduction]
73 questions
168
votes
2 answers
What are some uses of decltype(auto)?
In c++14 the decltype(auto) idiom is introduced.
Typically its use is to allow auto declarations to use the decltype rules on the given expression.
Searching for examples of "good" usage of the idiom I can only think of things like the following…
![](../../users/profiles/2567683.webp)
Nikos Athanasiou
- 24,831
- 11
- 72
- 136
153
votes
7 answers
When should I use C++14 automatic return type deduction?
With GCC 4.8.0 released, we have a compiler that supports automatic return type deduction, part of C++14. With -std=c++1y, I can do this:
auto foo() { //deduced to be int
return 5;
}
My question is: When should I use this feature? When is it…
![](../../users/profiles/962089.webp)
chris
- 55,166
- 13
- 130
- 185
39
votes
1 answer
Why does auto return type deduction work with not fully defined types?
Consider the following:
template
struct Base {
// NOTE: if I replace the decltype(...) below with auto, code compiles
decltype(&Der::operator()) getCallOperator() const {
return &Der::operator();
}
};
struct Foo :…
![](../../users/profiles/2617356.webp)
Michał W. Urbańczyk
- 1,393
- 11
- 20
34
votes
2 answers
Does auto return type deduction force multiple functions to have the same return type?
Consider the below snippet:
struct A
{
auto foo(), bar();
};
auto A::foo() { return 1; }
auto A::bar() { return 'a'; }
int main()
{
}
It compiles fine in Clang++ 3.7.0.
It fails in G++ 5.2.0:
main.cpp: In member function 'auto…
![](../../users/profiles/391022.webp)
Marc Andreson
- 3,225
- 5
- 31
- 51
33
votes
5 answers
Why can the return type of main not be deduced?
As expected, the following fails in C++11 because that language does not have return type deduction for bog standard functions:
auto main()
{
return 0;
}
However, C++14 does, so I cannot explain the following error (with equivalent outcomes in…
![](../../users/profiles/560648.webp)
Lightness Races in Orbit
- 358,771
- 68
- 593
- 989
30
votes
1 answer
Return type deduction for in-class friend functions
Here is a little experiment with return type deduction for in-class friend functions (using Clang 3.4 SVN and g++ 4.8.1 with std=c++1y in both cases) that is not documented in the linked working paper
#include
struct A
{
int a_;
…
![](../../users/profiles/819272.webp)
TemplateRex
- 65,583
- 16
- 147
- 283
28
votes
2 answers
Is void{} legal or not?
This is a follow-up up of this question.
In the comments and in the answer it is said more than once that void{} is neither a valid type-id nor a valid expression.
That was fine, it made sense and that was all.
Then I came through [7.1.7.4.1/2]…
![](../../users/profiles/4987285.webp)
skypjack
- 45,296
- 16
- 80
- 161
27
votes
3 answers
What is the return type of a lambda expression if an item of a vector is returned?
Consider the following snippet:
#include
#include
#include
int main()
{
std::vectorv = {0,1,2,3,4,5,6};
std::function f = [&v](int i) { return v[i];};
std::function
![](../../users/profiles/4040558.webp)
Jing Li
- 599
- 4
- 14
24
votes
1 answer
Why does auto return type change the overload resolution?
Thanks to decltype as a return type, C++11 made it extremely easy to introduce decorators. For instance, consider this class:
struct base
{
void fun(unsigned) {}
};
I want to decorate it with additional features, and since I will do that several…
![](../../users/profiles/1353549.webp)
akim
- 6,950
- 1
- 38
- 51
22
votes
1 answer
decltype deducted result of in-class defined function
Why does
struct MyStruct {
auto foo () { return 1; }
auto bar () { return foo(); }
};
compile, but when using a trailing return type like so:
struct MyStruct {
auto foo () { return 1; }
auto bar () -> decltype(foo()) { return foo();…
![](../../users/profiles/7341388.webp)
zatm8
- 223
- 1
- 7
18
votes
3 answers
Three-way comparison operator with inconsistent ordering deduction
Some time ago I defined my first three-way comparison operator. It compared a single type and replaced multiple conventional operators. Great feature. Then I tried to implement a similar operator for comparing two variants by delegation:
auto…
![](../../users/profiles/1421332.webp)
Silicomancer
- 7,280
- 6
- 49
- 101
18
votes
1 answer
Call operator with auto return type being chosen instead of constructor when using std::function
The following snippet:
#include
struct X {
X(std::function fn); // (1)
X(double, double); // (2)
template
auto operator()(T const& t) const { // (3)
return t.foo();
…
![](../../users/profiles/2666289.webp)
Holt
- 32,271
- 6
- 74
- 116
18
votes
3 answers
How to typedef the return type of a method from a template class?
I have a templated class Helper which looks like this:
template< typename Mapper >
class Helper
{
public:
using mappedType = ... ;
};
I would need mappedType to be the type returned by the map(const int&) method in the Mapper class. Given a…
![](../../users/profiles/1948931.webp)
Dr_Sam
- 1,688
- 14
- 24
18
votes
2 answers
Return type deduction with a private member variable
As was explained in this Q&A yesterday, both g++ 4.8 and Clang 3.3 correctly complain about the code below with an error like "'b_' was not declared in this scope"
#include
class Test
{
public:
Test(): b_(0) {}
auto foo() const…
![](../../users/profiles/819272.webp)
TemplateRex
- 65,583
- 16
- 147
- 283
15
votes
2 answers
CRTP and c++1y return type deduction
I was recently playing with CRTP when I came across something that surprised me when used with c++1y functions whose type is deduced. The following code works:
template
struct Base
{
auto foo()
{
return…
![](../../users/profiles/1364752.webp)
Morwenn
- 19,202
- 10
- 89
- 142