Return vector as auto from function

The name of the pictureThe name of the pictureThe name of the pictureClash 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:



  1. error: use of auto retVec() before deduction of auto --->
    auto ret_vec = retVec(**)**;

  2. error: unable to deduce auto&& from ret_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.










share|improve this question



















  • 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















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:



  1. error: use of auto retVec() before deduction of auto --->
    auto ret_vec = retVec(**)**;

  2. error: unable to deduce auto&& from ret_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.










share|improve this question



















  • 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













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:



  1. error: use of auto retVec() before deduction of auto --->
    auto ret_vec = retVec(**)**;

  2. error: unable to deduce auto&& from ret_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.










share|improve this question















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:



  1. error: use of auto retVec() before deduction of auto --->
    auto ret_vec = retVec(**)**;

  2. error: unable to deduce auto&& from ret_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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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













  • 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













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 keyword auto indicates that the return type will be
deduced from the operand of its return statement using the rules for
template argument deduction.







share|improve this answer


















  • 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 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






  • 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


















up vote
3
down vote













This works since C++14 (see here) and not with C++11 (here).






share|improve this answer




















  • thank you for reply, I update question. My mistake in formulating the question.
    – Max
    19 mins ago

















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).






share|improve this answer




















  • thank you for reply, I update question. My mistake in formulating the question.
    – Max
    36 mins ago










Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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 keyword auto indicates that the return type will be
deduced from the operand of its return statement using the rules for
template argument deduction.







share|improve this answer


















  • 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 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






  • 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















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 keyword auto indicates that the return type will be
deduced from the operand of its return statement using the rules for
template argument deduction.







share|improve this answer


















  • 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 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






  • 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













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 keyword auto indicates that the return type will be
deduced from the operand of its return statement using the rules for
template argument deduction.







share|improve this answer














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 keyword auto indicates that the return type will be
deduced from the operand of its return statement using the rules for
template argument deduction.








share|improve this answer














share|improve this answer



share|improve this answer








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 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






  • 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




    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 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






  • 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













up vote
3
down vote













This works since C++14 (see here) and not with C++11 (here).






share|improve this answer




















  • thank you for reply, I update question. My mistake in formulating the question.
    – Max
    19 mins ago














up vote
3
down vote













This works since C++14 (see here) and not with C++11 (here).






share|improve this answer




















  • thank you for reply, I update question. My mistake in formulating the question.
    – Max
    19 mins ago












up vote
3
down vote










up vote
3
down vote









This works since C++14 (see here) and not with C++11 (here).






share|improve this answer












This works since C++14 (see here) and not with C++11 (here).







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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










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).






share|improve this answer




















  • thank you for reply, I update question. My mistake in formulating the question.
    – Max
    36 mins ago














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).






share|improve this answer




















  • thank you for reply, I update question. My mistake in formulating the question.
    – Max
    36 mins ago












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).






share|improve this answer












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).







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

One-line joke