What do we need std::as_const() for?












16















C++11 has given us std::add_const; with C++17, we have a new structure - std::as_const(). The former just tacks a const before the type you provide it with. The second one is a proper (template of a) function, not type trait, which seems to do the same - except for when the type is an rvalue-reference, in which case it cannot be used.



I don't quite understand the motivation for providing std::as_const(). Why do we need it in addition to std::add_const?










share|improve this question

























  • I like how you ask questions and then answer to yourself in the very same minute. Surprised you haven't accepted your own answer yet.

    – Maxim Egorushkin
    Nov 23 '18 at 17:19








  • 5





    @MaximEgorushkin see meta.stackoverflow.com/q/250204/1639256

    – Oktalist
    Nov 23 '18 at 17:55






  • 1





    @MaximEgorushkin and other readers: This is intentional, and is actually encouraged here on SO. I was actually planning to just ask this, but as I got an idea on how to find the right standard committee proposal paper. I found it and still needed clarification, so I answered what I did understand; then with commenters' help I was able to complete the answer. The result is a useful question and answer.

    – einpoklum
    Nov 23 '18 at 22:53






  • 3





    @MaximEgorushkin: SO have a feature for exactly this. Check it out: "Ask Question", and there's a checkbox at the bottom of the page: "answer your own question". If you have a better answer than einpoklum's one, feel free to add it.

    – geza
    Nov 23 '18 at 23:05






  • 1





    @MaximEgorushkin: If you're suggesting that the reputation is the reward, then - you're assigning it too much significance. It is just a "gameification" mechanism to promote beneficial activity here on the site. I would lie to you if I said I don't get a slight ego boost from being up-voted - but this is not money, or candy, or anything like that.

    – einpoklum
    Nov 23 '18 at 23:28
















16















C++11 has given us std::add_const; with C++17, we have a new structure - std::as_const(). The former just tacks a const before the type you provide it with. The second one is a proper (template of a) function, not type trait, which seems to do the same - except for when the type is an rvalue-reference, in which case it cannot be used.



I don't quite understand the motivation for providing std::as_const(). Why do we need it in addition to std::add_const?










share|improve this question

























  • I like how you ask questions and then answer to yourself in the very same minute. Surprised you haven't accepted your own answer yet.

    – Maxim Egorushkin
    Nov 23 '18 at 17:19








  • 5





    @MaximEgorushkin see meta.stackoverflow.com/q/250204/1639256

    – Oktalist
    Nov 23 '18 at 17:55






  • 1





    @MaximEgorushkin and other readers: This is intentional, and is actually encouraged here on SO. I was actually planning to just ask this, but as I got an idea on how to find the right standard committee proposal paper. I found it and still needed clarification, so I answered what I did understand; then with commenters' help I was able to complete the answer. The result is a useful question and answer.

    – einpoklum
    Nov 23 '18 at 22:53






  • 3





    @MaximEgorushkin: SO have a feature for exactly this. Check it out: "Ask Question", and there's a checkbox at the bottom of the page: "answer your own question". If you have a better answer than einpoklum's one, feel free to add it.

    – geza
    Nov 23 '18 at 23:05






  • 1





    @MaximEgorushkin: If you're suggesting that the reputation is the reward, then - you're assigning it too much significance. It is just a "gameification" mechanism to promote beneficial activity here on the site. I would lie to you if I said I don't get a slight ego boost from being up-voted - but this is not money, or candy, or anything like that.

    – einpoklum
    Nov 23 '18 at 23:28














16












16








16


1






C++11 has given us std::add_const; with C++17, we have a new structure - std::as_const(). The former just tacks a const before the type you provide it with. The second one is a proper (template of a) function, not type trait, which seems to do the same - except for when the type is an rvalue-reference, in which case it cannot be used.



I don't quite understand the motivation for providing std::as_const(). Why do we need it in addition to std::add_const?










share|improve this question
















C++11 has given us std::add_const; with C++17, we have a new structure - std::as_const(). The former just tacks a const before the type you provide it with. The second one is a proper (template of a) function, not type trait, which seems to do the same - except for when the type is an rvalue-reference, in which case it cannot be used.



I don't quite understand the motivation for providing std::as_const(). Why do we need it in addition to std::add_const?







c++ const c++17 typetraits rationale






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 25 '18 at 12:40







einpoklum

















asked Nov 23 '18 at 16:46









einpoklumeinpoklum

34.7k27128247




34.7k27128247













  • I like how you ask questions and then answer to yourself in the very same minute. Surprised you haven't accepted your own answer yet.

    – Maxim Egorushkin
    Nov 23 '18 at 17:19








  • 5





    @MaximEgorushkin see meta.stackoverflow.com/q/250204/1639256

    – Oktalist
    Nov 23 '18 at 17:55






  • 1





    @MaximEgorushkin and other readers: This is intentional, and is actually encouraged here on SO. I was actually planning to just ask this, but as I got an idea on how to find the right standard committee proposal paper. I found it and still needed clarification, so I answered what I did understand; then with commenters' help I was able to complete the answer. The result is a useful question and answer.

    – einpoklum
    Nov 23 '18 at 22:53






  • 3





    @MaximEgorushkin: SO have a feature for exactly this. Check it out: "Ask Question", and there's a checkbox at the bottom of the page: "answer your own question". If you have a better answer than einpoklum's one, feel free to add it.

    – geza
    Nov 23 '18 at 23:05






  • 1





    @MaximEgorushkin: If you're suggesting that the reputation is the reward, then - you're assigning it too much significance. It is just a "gameification" mechanism to promote beneficial activity here on the site. I would lie to you if I said I don't get a slight ego boost from being up-voted - but this is not money, or candy, or anything like that.

    – einpoklum
    Nov 23 '18 at 23:28



















  • I like how you ask questions and then answer to yourself in the very same minute. Surprised you haven't accepted your own answer yet.

    – Maxim Egorushkin
    Nov 23 '18 at 17:19








  • 5





    @MaximEgorushkin see meta.stackoverflow.com/q/250204/1639256

    – Oktalist
    Nov 23 '18 at 17:55






  • 1





    @MaximEgorushkin and other readers: This is intentional, and is actually encouraged here on SO. I was actually planning to just ask this, but as I got an idea on how to find the right standard committee proposal paper. I found it and still needed clarification, so I answered what I did understand; then with commenters' help I was able to complete the answer. The result is a useful question and answer.

    – einpoklum
    Nov 23 '18 at 22:53






  • 3





    @MaximEgorushkin: SO have a feature for exactly this. Check it out: "Ask Question", and there's a checkbox at the bottom of the page: "answer your own question". If you have a better answer than einpoklum's one, feel free to add it.

    – geza
    Nov 23 '18 at 23:05






  • 1





    @MaximEgorushkin: If you're suggesting that the reputation is the reward, then - you're assigning it too much significance. It is just a "gameification" mechanism to promote beneficial activity here on the site. I would lie to you if I said I don't get a slight ego boost from being up-voted - but this is not money, or candy, or anything like that.

    – einpoklum
    Nov 23 '18 at 23:28

















I like how you ask questions and then answer to yourself in the very same minute. Surprised you haven't accepted your own answer yet.

– Maxim Egorushkin
Nov 23 '18 at 17:19







I like how you ask questions and then answer to yourself in the very same minute. Surprised you haven't accepted your own answer yet.

– Maxim Egorushkin
Nov 23 '18 at 17:19






5




5





@MaximEgorushkin see meta.stackoverflow.com/q/250204/1639256

– Oktalist
Nov 23 '18 at 17:55





@MaximEgorushkin see meta.stackoverflow.com/q/250204/1639256

– Oktalist
Nov 23 '18 at 17:55




1




1





@MaximEgorushkin and other readers: This is intentional, and is actually encouraged here on SO. I was actually planning to just ask this, but as I got an idea on how to find the right standard committee proposal paper. I found it and still needed clarification, so I answered what I did understand; then with commenters' help I was able to complete the answer. The result is a useful question and answer.

– einpoklum
Nov 23 '18 at 22:53





@MaximEgorushkin and other readers: This is intentional, and is actually encouraged here on SO. I was actually planning to just ask this, but as I got an idea on how to find the right standard committee proposal paper. I found it and still needed clarification, so I answered what I did understand; then with commenters' help I was able to complete the answer. The result is a useful question and answer.

– einpoklum
Nov 23 '18 at 22:53




3




3





@MaximEgorushkin: SO have a feature for exactly this. Check it out: "Ask Question", and there's a checkbox at the bottom of the page: "answer your own question". If you have a better answer than einpoklum's one, feel free to add it.

– geza
Nov 23 '18 at 23:05





@MaximEgorushkin: SO have a feature for exactly this. Check it out: "Ask Question", and there's a checkbox at the bottom of the page: "answer your own question". If you have a better answer than einpoklum's one, feel free to add it.

– geza
Nov 23 '18 at 23:05




1




1





@MaximEgorushkin: If you're suggesting that the reputation is the reward, then - you're assigning it too much significance. It is just a "gameification" mechanism to promote beneficial activity here on the site. I would lie to you if I said I don't get a slight ego boost from being up-voted - but this is not money, or candy, or anything like that.

– einpoklum
Nov 23 '18 at 23:28





@MaximEgorushkin: If you're suggesting that the reputation is the reward, then - you're assigning it too much significance. It is just a "gameification" mechanism to promote beneficial activity here on the site. I would lie to you if I said I don't get a slight ego boost from being up-voted - but this is not money, or candy, or anything like that.

– einpoklum
Nov 23 '18 at 23:28












1 Answer
1






active

oldest

votes


















19














"Need" is a strong word... std::as_const exists because it's useful, not strictly necessary. Since it's a function rather than a trait, we can use it to "add const" to actual values rather than to types.



More specifically: Suppose I have some variable my_value and I want to treat it as a const, but not copy it. Before C++17 I would need to write:



static_cast<const MyType&>(my_value)


and if I don't want to specify the type explicitly, it would be:



static_cast<std::add_const_t<std::remove_reference_t<decltype(my_value)>> &>(my_value)


or if you want to get down and dirty, and use C-style casting:



(const decltype(my_value) &) (my_value)


all of which are annoying and verbose.



Instead of these, with C++17 now write std::as_const(my_value) and that's all there is to it.



Notes:





  • This function is disabled for rvalue references even though it works just fine for them. The reason is to help you avoid inadvertantly keeping a reference to a temporary past its destruction. As @NicolBolas explains, if you write something like:



    for(auto &x : std::as_const(returns_container())) { /* do stuff with x */ }


    then the returned container's lifetime ends before the first iteration of the loop. Very easy to miss!



  • For additional (?) information, consult the official proposition of this utility function: P007R1, by Adam David Alan Martin and Alisdair Meredith.







share|improve this answer





















  • 2





    Worse still: you need std::remove_reference_t if my_value is itself a reference.

    – Quentin
    Nov 23 '18 at 16:49






  • 1





    "I'm not entirely clear why the deletion for rvalue references is necessary though." Because C++'s temporary lifetime extension rules don't work correctly if you tried to use as_const on them. If you ever call as_const on a temporary, you will get a reference to a destroyed object.

    – Nicol Bolas
    Nov 23 '18 at 16:53








  • 1





    @NicolBolas: Thanks. But - won't the temporary live until the the statement in which the as_const() is used, is fully executed? If not, can you link to an explanation?

    – einpoklum
    Nov 23 '18 at 16:57






  • 2





    @einpoklum: It will life to the end of the full expression. But if you're doing something like for(auto &val : std::as_const(returns_container())), the "full expression" ends before the for loop starts. Whereas if you had used returns_container() directly, the temporary's lifetime would extend to the entire for loop. Better to prevent people from making that mistake up-front.

    – Nicol Bolas
    Nov 23 '18 at 17:02








  • 1





    std::as_const() is particularly useful to select the const overload of a set. Given std::map<int,int> m, m[0] and as_const(m)[0] are quite different.

    – YSC
    Nov 23 '18 at 17:14











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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
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%2f53450366%2fwhat-do-we-need-stdas-const-for%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









19














"Need" is a strong word... std::as_const exists because it's useful, not strictly necessary. Since it's a function rather than a trait, we can use it to "add const" to actual values rather than to types.



More specifically: Suppose I have some variable my_value and I want to treat it as a const, but not copy it. Before C++17 I would need to write:



static_cast<const MyType&>(my_value)


and if I don't want to specify the type explicitly, it would be:



static_cast<std::add_const_t<std::remove_reference_t<decltype(my_value)>> &>(my_value)


or if you want to get down and dirty, and use C-style casting:



(const decltype(my_value) &) (my_value)


all of which are annoying and verbose.



Instead of these, with C++17 now write std::as_const(my_value) and that's all there is to it.



Notes:





  • This function is disabled for rvalue references even though it works just fine for them. The reason is to help you avoid inadvertantly keeping a reference to a temporary past its destruction. As @NicolBolas explains, if you write something like:



    for(auto &x : std::as_const(returns_container())) { /* do stuff with x */ }


    then the returned container's lifetime ends before the first iteration of the loop. Very easy to miss!



  • For additional (?) information, consult the official proposition of this utility function: P007R1, by Adam David Alan Martin and Alisdair Meredith.







share|improve this answer





















  • 2





    Worse still: you need std::remove_reference_t if my_value is itself a reference.

    – Quentin
    Nov 23 '18 at 16:49






  • 1





    "I'm not entirely clear why the deletion for rvalue references is necessary though." Because C++'s temporary lifetime extension rules don't work correctly if you tried to use as_const on them. If you ever call as_const on a temporary, you will get a reference to a destroyed object.

    – Nicol Bolas
    Nov 23 '18 at 16:53








  • 1





    @NicolBolas: Thanks. But - won't the temporary live until the the statement in which the as_const() is used, is fully executed? If not, can you link to an explanation?

    – einpoklum
    Nov 23 '18 at 16:57






  • 2





    @einpoklum: It will life to the end of the full expression. But if you're doing something like for(auto &val : std::as_const(returns_container())), the "full expression" ends before the for loop starts. Whereas if you had used returns_container() directly, the temporary's lifetime would extend to the entire for loop. Better to prevent people from making that mistake up-front.

    – Nicol Bolas
    Nov 23 '18 at 17:02








  • 1





    std::as_const() is particularly useful to select the const overload of a set. Given std::map<int,int> m, m[0] and as_const(m)[0] are quite different.

    – YSC
    Nov 23 '18 at 17:14
















19














"Need" is a strong word... std::as_const exists because it's useful, not strictly necessary. Since it's a function rather than a trait, we can use it to "add const" to actual values rather than to types.



More specifically: Suppose I have some variable my_value and I want to treat it as a const, but not copy it. Before C++17 I would need to write:



static_cast<const MyType&>(my_value)


and if I don't want to specify the type explicitly, it would be:



static_cast<std::add_const_t<std::remove_reference_t<decltype(my_value)>> &>(my_value)


or if you want to get down and dirty, and use C-style casting:



(const decltype(my_value) &) (my_value)


all of which are annoying and verbose.



Instead of these, with C++17 now write std::as_const(my_value) and that's all there is to it.



Notes:





  • This function is disabled for rvalue references even though it works just fine for them. The reason is to help you avoid inadvertantly keeping a reference to a temporary past its destruction. As @NicolBolas explains, if you write something like:



    for(auto &x : std::as_const(returns_container())) { /* do stuff with x */ }


    then the returned container's lifetime ends before the first iteration of the loop. Very easy to miss!



  • For additional (?) information, consult the official proposition of this utility function: P007R1, by Adam David Alan Martin and Alisdair Meredith.







share|improve this answer





















  • 2





    Worse still: you need std::remove_reference_t if my_value is itself a reference.

    – Quentin
    Nov 23 '18 at 16:49






  • 1





    "I'm not entirely clear why the deletion for rvalue references is necessary though." Because C++'s temporary lifetime extension rules don't work correctly if you tried to use as_const on them. If you ever call as_const on a temporary, you will get a reference to a destroyed object.

    – Nicol Bolas
    Nov 23 '18 at 16:53








  • 1





    @NicolBolas: Thanks. But - won't the temporary live until the the statement in which the as_const() is used, is fully executed? If not, can you link to an explanation?

    – einpoklum
    Nov 23 '18 at 16:57






  • 2





    @einpoklum: It will life to the end of the full expression. But if you're doing something like for(auto &val : std::as_const(returns_container())), the "full expression" ends before the for loop starts. Whereas if you had used returns_container() directly, the temporary's lifetime would extend to the entire for loop. Better to prevent people from making that mistake up-front.

    – Nicol Bolas
    Nov 23 '18 at 17:02








  • 1





    std::as_const() is particularly useful to select the const overload of a set. Given std::map<int,int> m, m[0] and as_const(m)[0] are quite different.

    – YSC
    Nov 23 '18 at 17:14














19












19








19







"Need" is a strong word... std::as_const exists because it's useful, not strictly necessary. Since it's a function rather than a trait, we can use it to "add const" to actual values rather than to types.



More specifically: Suppose I have some variable my_value and I want to treat it as a const, but not copy it. Before C++17 I would need to write:



static_cast<const MyType&>(my_value)


and if I don't want to specify the type explicitly, it would be:



static_cast<std::add_const_t<std::remove_reference_t<decltype(my_value)>> &>(my_value)


or if you want to get down and dirty, and use C-style casting:



(const decltype(my_value) &) (my_value)


all of which are annoying and verbose.



Instead of these, with C++17 now write std::as_const(my_value) and that's all there is to it.



Notes:





  • This function is disabled for rvalue references even though it works just fine for them. The reason is to help you avoid inadvertantly keeping a reference to a temporary past its destruction. As @NicolBolas explains, if you write something like:



    for(auto &x : std::as_const(returns_container())) { /* do stuff with x */ }


    then the returned container's lifetime ends before the first iteration of the loop. Very easy to miss!



  • For additional (?) information, consult the official proposition of this utility function: P007R1, by Adam David Alan Martin and Alisdair Meredith.







share|improve this answer















"Need" is a strong word... std::as_const exists because it's useful, not strictly necessary. Since it's a function rather than a trait, we can use it to "add const" to actual values rather than to types.



More specifically: Suppose I have some variable my_value and I want to treat it as a const, but not copy it. Before C++17 I would need to write:



static_cast<const MyType&>(my_value)


and if I don't want to specify the type explicitly, it would be:



static_cast<std::add_const_t<std::remove_reference_t<decltype(my_value)>> &>(my_value)


or if you want to get down and dirty, and use C-style casting:



(const decltype(my_value) &) (my_value)


all of which are annoying and verbose.



Instead of these, with C++17 now write std::as_const(my_value) and that's all there is to it.



Notes:





  • This function is disabled for rvalue references even though it works just fine for them. The reason is to help you avoid inadvertantly keeping a reference to a temporary past its destruction. As @NicolBolas explains, if you write something like:



    for(auto &x : std::as_const(returns_container())) { /* do stuff with x */ }


    then the returned container's lifetime ends before the first iteration of the loop. Very easy to miss!



  • For additional (?) information, consult the official proposition of this utility function: P007R1, by Adam David Alan Martin and Alisdair Meredith.








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 13:37









Arne Vogel

4,49521326




4,49521326










answered Nov 23 '18 at 16:46









einpoklumeinpoklum

34.7k27128247




34.7k27128247








  • 2





    Worse still: you need std::remove_reference_t if my_value is itself a reference.

    – Quentin
    Nov 23 '18 at 16:49






  • 1





    "I'm not entirely clear why the deletion for rvalue references is necessary though." Because C++'s temporary lifetime extension rules don't work correctly if you tried to use as_const on them. If you ever call as_const on a temporary, you will get a reference to a destroyed object.

    – Nicol Bolas
    Nov 23 '18 at 16:53








  • 1





    @NicolBolas: Thanks. But - won't the temporary live until the the statement in which the as_const() is used, is fully executed? If not, can you link to an explanation?

    – einpoklum
    Nov 23 '18 at 16:57






  • 2





    @einpoklum: It will life to the end of the full expression. But if you're doing something like for(auto &val : std::as_const(returns_container())), the "full expression" ends before the for loop starts. Whereas if you had used returns_container() directly, the temporary's lifetime would extend to the entire for loop. Better to prevent people from making that mistake up-front.

    – Nicol Bolas
    Nov 23 '18 at 17:02








  • 1





    std::as_const() is particularly useful to select the const overload of a set. Given std::map<int,int> m, m[0] and as_const(m)[0] are quite different.

    – YSC
    Nov 23 '18 at 17:14














  • 2





    Worse still: you need std::remove_reference_t if my_value is itself a reference.

    – Quentin
    Nov 23 '18 at 16:49






  • 1





    "I'm not entirely clear why the deletion for rvalue references is necessary though." Because C++'s temporary lifetime extension rules don't work correctly if you tried to use as_const on them. If you ever call as_const on a temporary, you will get a reference to a destroyed object.

    – Nicol Bolas
    Nov 23 '18 at 16:53








  • 1





    @NicolBolas: Thanks. But - won't the temporary live until the the statement in which the as_const() is used, is fully executed? If not, can you link to an explanation?

    – einpoklum
    Nov 23 '18 at 16:57






  • 2





    @einpoklum: It will life to the end of the full expression. But if you're doing something like for(auto &val : std::as_const(returns_container())), the "full expression" ends before the for loop starts. Whereas if you had used returns_container() directly, the temporary's lifetime would extend to the entire for loop. Better to prevent people from making that mistake up-front.

    – Nicol Bolas
    Nov 23 '18 at 17:02








  • 1





    std::as_const() is particularly useful to select the const overload of a set. Given std::map<int,int> m, m[0] and as_const(m)[0] are quite different.

    – YSC
    Nov 23 '18 at 17:14








2




2





Worse still: you need std::remove_reference_t if my_value is itself a reference.

– Quentin
Nov 23 '18 at 16:49





Worse still: you need std::remove_reference_t if my_value is itself a reference.

– Quentin
Nov 23 '18 at 16:49




1




1





"I'm not entirely clear why the deletion for rvalue references is necessary though." Because C++'s temporary lifetime extension rules don't work correctly if you tried to use as_const on them. If you ever call as_const on a temporary, you will get a reference to a destroyed object.

– Nicol Bolas
Nov 23 '18 at 16:53







"I'm not entirely clear why the deletion for rvalue references is necessary though." Because C++'s temporary lifetime extension rules don't work correctly if you tried to use as_const on them. If you ever call as_const on a temporary, you will get a reference to a destroyed object.

– Nicol Bolas
Nov 23 '18 at 16:53






1




1





@NicolBolas: Thanks. But - won't the temporary live until the the statement in which the as_const() is used, is fully executed? If not, can you link to an explanation?

– einpoklum
Nov 23 '18 at 16:57





@NicolBolas: Thanks. But - won't the temporary live until the the statement in which the as_const() is used, is fully executed? If not, can you link to an explanation?

– einpoklum
Nov 23 '18 at 16:57




2




2





@einpoklum: It will life to the end of the full expression. But if you're doing something like for(auto &val : std::as_const(returns_container())), the "full expression" ends before the for loop starts. Whereas if you had used returns_container() directly, the temporary's lifetime would extend to the entire for loop. Better to prevent people from making that mistake up-front.

– Nicol Bolas
Nov 23 '18 at 17:02







@einpoklum: It will life to the end of the full expression. But if you're doing something like for(auto &val : std::as_const(returns_container())), the "full expression" ends before the for loop starts. Whereas if you had used returns_container() directly, the temporary's lifetime would extend to the entire for loop. Better to prevent people from making that mistake up-front.

– Nicol Bolas
Nov 23 '18 at 17:02






1




1





std::as_const() is particularly useful to select the const overload of a set. Given std::map<int,int> m, m[0] and as_const(m)[0] are quite different.

– YSC
Nov 23 '18 at 17:14





std::as_const() is particularly useful to select the const overload of a set. Given std::map<int,int> m, m[0] and as_const(m)[0] are quite different.

– YSC
Nov 23 '18 at 17:14




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450366%2fwhat-do-we-need-stdas-const-for%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin