Return vector as auto from function
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
Is it possible to return std::vector as auto?
For example:
auto retVec()
std::vector<int> vec_l;
l.push_back(1);
l.push_back(2);
return vec_l;
...
auto ret_vec = retVec();
for (auto& it : ret_vec)
when I write something like this I get an error:
- error: use of
auto retVec()
before deduction ofauto
--->
auto ret_vec = retVec(**)**;
- error: unable to deduce
auto&&
fromret_vec
--->for (auto it :
**ret_vec**) {
How do I actually write this?
UPDATE:
I'm sorry. I use this retVec as method in class and it doesn't work. When I use it as function in class - everything work fine. My mistake in formulating the question.
c++ c++11
add a comment |Â
up vote
7
down vote
favorite
Is it possible to return std::vector as auto?
For example:
auto retVec()
std::vector<int> vec_l;
l.push_back(1);
l.push_back(2);
return vec_l;
...
auto ret_vec = retVec();
for (auto& it : ret_vec)
when I write something like this I get an error:
- error: use of
auto retVec()
before deduction ofauto
--->
auto ret_vec = retVec(**)**;
- error: unable to deduce
auto&&
fromret_vec
--->for (auto it :
**ret_vec**) {
How do I actually write this?
UPDATE:
I'm sorry. I use this retVec as method in class and it doesn't work. When I use it as function in class - everything work fine. My mistake in formulating the question.
c++ c++11
4
Works for me
– tkausl
1 hour ago
2
How are you compiling this?
– UnholySheep
1 hour ago
3
For posterity, can you add the compiler version and command you ran to compile as part of the question?
– doron
1 hour ago
3
This is an instance when adding all the tags is detrimental to your question. This simply cannot be valid C++11.
– StoryTeller
1 hour ago
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
Is it possible to return std::vector as auto?
For example:
auto retVec()
std::vector<int> vec_l;
l.push_back(1);
l.push_back(2);
return vec_l;
...
auto ret_vec = retVec();
for (auto& it : ret_vec)
when I write something like this I get an error:
- error: use of
auto retVec()
before deduction ofauto
--->
auto ret_vec = retVec(**)**;
- error: unable to deduce
auto&&
fromret_vec
--->for (auto it :
**ret_vec**) {
How do I actually write this?
UPDATE:
I'm sorry. I use this retVec as method in class and it doesn't work. When I use it as function in class - everything work fine. My mistake in formulating the question.
c++ c++11
Is it possible to return std::vector as auto?
For example:
auto retVec()
std::vector<int> vec_l;
l.push_back(1);
l.push_back(2);
return vec_l;
...
auto ret_vec = retVec();
for (auto& it : ret_vec)
when I write something like this I get an error:
- error: use of
auto retVec()
before deduction ofauto
--->
auto ret_vec = retVec(**)**;
- error: unable to deduce
auto&&
fromret_vec
--->for (auto it :
**ret_vec**) {
How do I actually write this?
UPDATE:
I'm sorry. I use this retVec as method in class and it doesn't work. When I use it as function in class - everything work fine. My mistake in formulating the question.
c++ c++11
c++ c++11
edited 47 mins ago
asked 1 hour ago


Max
3951522
3951522
4
Works for me
– tkausl
1 hour ago
2
How are you compiling this?
– UnholySheep
1 hour ago
3
For posterity, can you add the compiler version and command you ran to compile as part of the question?
– doron
1 hour ago
3
This is an instance when adding all the tags is detrimental to your question. This simply cannot be valid C++11.
– StoryTeller
1 hour ago
add a comment |Â
4
Works for me
– tkausl
1 hour ago
2
How are you compiling this?
– UnholySheep
1 hour ago
3
For posterity, can you add the compiler version and command you ran to compile as part of the question?
– doron
1 hour ago
3
This is an instance when adding all the tags is detrimental to your question. This simply cannot be valid C++11.
– StoryTeller
1 hour ago
4
4
Works for me
– tkausl
1 hour ago
Works for me
– tkausl
1 hour ago
2
2
How are you compiling this?
– UnholySheep
1 hour ago
How are you compiling this?
– UnholySheep
1 hour ago
3
3
For posterity, can you add the compiler version and command you ran to compile as part of the question?
– doron
1 hour ago
For posterity, can you add the compiler version and command you ran to compile as part of the question?
– doron
1 hour ago
3
3
This is an instance when adding all the tags is detrimental to your question. This simply cannot be valid C++11.
– StoryTeller
1 hour ago
This is an instance when adding all the tags is detrimental to your question. This simply cannot be valid C++11.
– StoryTeller
1 hour ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
11
down vote
accepted
You are compiling for C++11 standard. You need to compile for at least the C++14 standard as the deduced return type is only available starting with C++14. The reference states:
In a function declaration that does not use the trailing return type
syntax, the keywordauto
indicates that the return type will be
deduced from the operand of itsreturn
statement using the rules for
template argument deduction.
1
that was some parallel work. :-)
– schorsch312
1 hour ago
1
Blazing fast : ) might want to edit this link into the answer: en.cppreference.com/w/cpp/language/auto, as it shows '(since C++14)' next toauto function
– SkepticalEmpiricist
1 hour ago
@Ron It strange, because I use g++ (Ubuntu 7.3.0-21ubuntu1~16.04) 7.3.0 and flag -std=c++17 :)
– Max
1 hour ago
1
@Max You are compiling for C++11 standard. The C++17 version does not produce the above error.
– Ron
1 hour ago
@Ron I'm sorry. I use this retVec as method in class and it doesn't work. When I use it as function in class - everything work fine. My mistake in formulating the question.
– Max
50 mins ago
add a comment |Â
up vote
3
down vote
This works since C++14 (see here) and not with C++11 (here).
thank you for reply, I update question. My mistake in formulating the question.
– Max
19 mins ago
add a comment |Â
up vote
3
down vote
You can see this error on Coliru when compiling with -std=c++11
, but this works as intended when compiled with -std=c++14
.
Note that gcc even outputs a hint at this:
main.cpp:8:13: note: deduced return type only available with -std=c++14 or -std=gnu++14
Deducted return type using auto
is indeed a C++14 feature, see Item (3).
thank you for reply, I update question. My mistake in formulating the question.
– Max
36 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
You are compiling for C++11 standard. You need to compile for at least the C++14 standard as the deduced return type is only available starting with C++14. The reference states:
In a function declaration that does not use the trailing return type
syntax, the keywordauto
indicates that the return type will be
deduced from the operand of itsreturn
statement using the rules for
template argument deduction.
1
that was some parallel work. :-)
– schorsch312
1 hour ago
1
Blazing fast : ) might want to edit this link into the answer: en.cppreference.com/w/cpp/language/auto, as it shows '(since C++14)' next toauto function
– SkepticalEmpiricist
1 hour ago
@Ron It strange, because I use g++ (Ubuntu 7.3.0-21ubuntu1~16.04) 7.3.0 and flag -std=c++17 :)
– Max
1 hour ago
1
@Max You are compiling for C++11 standard. The C++17 version does not produce the above error.
– Ron
1 hour ago
@Ron I'm sorry. I use this retVec as method in class and it doesn't work. When I use it as function in class - everything work fine. My mistake in formulating the question.
– Max
50 mins ago
add a comment |Â
up vote
11
down vote
accepted
You are compiling for C++11 standard. You need to compile for at least the C++14 standard as the deduced return type is only available starting with C++14. The reference states:
In a function declaration that does not use the trailing return type
syntax, the keywordauto
indicates that the return type will be
deduced from the operand of itsreturn
statement using the rules for
template argument deduction.
1
that was some parallel work. :-)
– schorsch312
1 hour ago
1
Blazing fast : ) might want to edit this link into the answer: en.cppreference.com/w/cpp/language/auto, as it shows '(since C++14)' next toauto function
– SkepticalEmpiricist
1 hour ago
@Ron It strange, because I use g++ (Ubuntu 7.3.0-21ubuntu1~16.04) 7.3.0 and flag -std=c++17 :)
– Max
1 hour ago
1
@Max You are compiling for C++11 standard. The C++17 version does not produce the above error.
– Ron
1 hour ago
@Ron I'm sorry. I use this retVec as method in class and it doesn't work. When I use it as function in class - everything work fine. My mistake in formulating the question.
– Max
50 mins ago
add a comment |Â
up vote
11
down vote
accepted
up vote
11
down vote
accepted
You are compiling for C++11 standard. You need to compile for at least the C++14 standard as the deduced return type is only available starting with C++14. The reference states:
In a function declaration that does not use the trailing return type
syntax, the keywordauto
indicates that the return type will be
deduced from the operand of itsreturn
statement using the rules for
template argument deduction.
You are compiling for C++11 standard. You need to compile for at least the C++14 standard as the deduced return type is only available starting with C++14. The reference states:
In a function declaration that does not use the trailing return type
syntax, the keywordauto
indicates that the return type will be
deduced from the operand of itsreturn
statement using the rules for
template argument deduction.
edited 1 hour ago
answered 1 hour ago


Ron
9,94221733
9,94221733
1
that was some parallel work. :-)
– schorsch312
1 hour ago
1
Blazing fast : ) might want to edit this link into the answer: en.cppreference.com/w/cpp/language/auto, as it shows '(since C++14)' next toauto function
– SkepticalEmpiricist
1 hour ago
@Ron It strange, because I use g++ (Ubuntu 7.3.0-21ubuntu1~16.04) 7.3.0 and flag -std=c++17 :)
– Max
1 hour ago
1
@Max You are compiling for C++11 standard. The C++17 version does not produce the above error.
– Ron
1 hour ago
@Ron I'm sorry. I use this retVec as method in class and it doesn't work. When I use it as function in class - everything work fine. My mistake in formulating the question.
– Max
50 mins ago
add a comment |Â
1
that was some parallel work. :-)
– schorsch312
1 hour ago
1
Blazing fast : ) might want to edit this link into the answer: en.cppreference.com/w/cpp/language/auto, as it shows '(since C++14)' next toauto function
– SkepticalEmpiricist
1 hour ago
@Ron It strange, because I use g++ (Ubuntu 7.3.0-21ubuntu1~16.04) 7.3.0 and flag -std=c++17 :)
– Max
1 hour ago
1
@Max You are compiling for C++11 standard. The C++17 version does not produce the above error.
– Ron
1 hour ago
@Ron I'm sorry. I use this retVec as method in class and it doesn't work. When I use it as function in class - everything work fine. My mistake in formulating the question.
– Max
50 mins ago
1
1
that was some parallel work. :-)
– schorsch312
1 hour ago
that was some parallel work. :-)
– schorsch312
1 hour ago
1
1
Blazing fast : ) might want to edit this link into the answer: en.cppreference.com/w/cpp/language/auto, as it shows '(since C++14)' next to
auto function
– SkepticalEmpiricist
1 hour ago
Blazing fast : ) might want to edit this link into the answer: en.cppreference.com/w/cpp/language/auto, as it shows '(since C++14)' next to
auto function
– SkepticalEmpiricist
1 hour ago
@Ron It strange, because I use g++ (Ubuntu 7.3.0-21ubuntu1~16.04) 7.3.0 and flag -std=c++17 :)
– Max
1 hour ago
@Ron It strange, because I use g++ (Ubuntu 7.3.0-21ubuntu1~16.04) 7.3.0 and flag -std=c++17 :)
– Max
1 hour ago
1
1
@Max You are compiling for C++11 standard. The C++17 version does not produce the above error.
– Ron
1 hour ago
@Max You are compiling for C++11 standard. The C++17 version does not produce the above error.
– Ron
1 hour ago
@Ron I'm sorry. I use this retVec as method in class and it doesn't work. When I use it as function in class - everything work fine. My mistake in formulating the question.
– Max
50 mins ago
@Ron I'm sorry. I use this retVec as method in class and it doesn't work. When I use it as function in class - everything work fine. My mistake in formulating the question.
– Max
50 mins ago
add a comment |Â
up vote
3
down vote
This works since C++14 (see here) and not with C++11 (here).
thank you for reply, I update question. My mistake in formulating the question.
– Max
19 mins ago
add a comment |Â
up vote
3
down vote
This works since C++14 (see here) and not with C++11 (here).
thank you for reply, I update question. My mistake in formulating the question.
– Max
19 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
This works since C++14 (see here) and not with C++11 (here).
This works since C++14 (see here) and not with C++11 (here).
answered 1 hour ago
schorsch312
1,96321027
1,96321027
thank you for reply, I update question. My mistake in formulating the question.
– Max
19 mins ago
add a comment |Â
thank you for reply, I update question. My mistake in formulating the question.
– Max
19 mins ago
thank you for reply, I update question. My mistake in formulating the question.
– Max
19 mins ago
thank you for reply, I update question. My mistake in formulating the question.
– Max
19 mins ago
add a comment |Â
up vote
3
down vote
You can see this error on Coliru when compiling with -std=c++11
, but this works as intended when compiled with -std=c++14
.
Note that gcc even outputs a hint at this:
main.cpp:8:13: note: deduced return type only available with -std=c++14 or -std=gnu++14
Deducted return type using auto
is indeed a C++14 feature, see Item (3).
thank you for reply, I update question. My mistake in formulating the question.
– Max
36 mins ago
add a comment |Â
up vote
3
down vote
You can see this error on Coliru when compiling with -std=c++11
, but this works as intended when compiled with -std=c++14
.
Note that gcc even outputs a hint at this:
main.cpp:8:13: note: deduced return type only available with -std=c++14 or -std=gnu++14
Deducted return type using auto
is indeed a C++14 feature, see Item (3).
thank you for reply, I update question. My mistake in formulating the question.
– Max
36 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You can see this error on Coliru when compiling with -std=c++11
, but this works as intended when compiled with -std=c++14
.
Note that gcc even outputs a hint at this:
main.cpp:8:13: note: deduced return type only available with -std=c++14 or -std=gnu++14
Deducted return type using auto
is indeed a C++14 feature, see Item (3).
You can see this error on Coliru when compiling with -std=c++11
, but this works as intended when compiled with -std=c++14
.
Note that gcc even outputs a hint at this:
main.cpp:8:13: note: deduced return type only available with -std=c++14 or -std=gnu++14
Deducted return type using auto
is indeed a C++14 feature, see Item (3).
answered 1 hour ago


JBL
9,12033464
9,12033464
thank you for reply, I update question. My mistake in formulating the question.
– Max
36 mins ago
add a comment |Â
thank you for reply, I update question. My mistake in formulating the question.
– Max
36 mins ago
thank you for reply, I update question. My mistake in formulating the question.
– Max
36 mins ago
thank you for reply, I update question. My mistake in formulating the question.
– Max
36 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52424834%2freturn-vector-as-auto-from-function%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
4
Works for me
– tkausl
1 hour ago
2
How are you compiling this?
– UnholySheep
1 hour ago
3
For posterity, can you add the compiler version and command you ran to compile as part of the question?
– doron
1 hour ago
3
This is an instance when adding all the tags is detrimental to your question. This simply cannot be valid C++11.
– StoryTeller
1 hour ago