Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9188989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:08:38+00:00 2026-06-17T20:08:38+00:00

Inside an algorithm, I want to create a lambda that accepts an element by

  • 0

Inside an algorithm, I want to create a lambda that accepts an element by reference-to-const:

template<typename Iterator>
void solve_world_hunger(Iterator it)
{
    auto lambda = [](const decltype(*it)& x){
        auto y = x;   // this should work
        x = x;        // this should fail
    };
}

The compiler does not like this code:

Error: »const«-qualifier cannot be applied to »int&« (translated manually from German)

Then I realized that decltype(*it) is already a reference, and of course those cannot be made const. If I remove the const, the code compiles, but I want x = x to fail.

Let us trust the programmer (which is me) for a minute and get rid of the const and the explicit &, which gets dropped due to reference collapsing rules, anyways. But wait, is decltype(*it) actually guaranteed to be a reference, or should I add the explicit & to be on the safe side?

If we do not trust the programmer, I can think two solutions to solve the problem:

(const typename std::remove_reference<decltype(*it)>::type& x)

(const typename std::iterator_traits<Iterator>::value_type& x)

You can decide for yourself which one is uglier. Ideally, I would want a solution that does not involve any template meta-programming, because my target audience has never heard of that before. So:

Question 1: Is decltype(*it)& always the same as decltype(*it)?

Question 2: How can I pass an element by reference-to-const without template meta-programming?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:08:39+00:00Added an answer on June 17, 2026 at 8:08 pm

    Question 1: no, the requirement on InputIterator is merely that *it is convertible to T (table 72, in “Iterator requirements”).

    So decltype(*it) could for example be const char& for an iterator whose value_type is int. Or it could be int. Or double.

    Using iterator_traits is not equivalent to using decltype, decide which you want.

    For the same reason, auto value = *it; does not necessarily give you a variable with the value type of the iterator.

    Question 2: might depend what you mean by template meta-programming.

    If using a traits type is TMP, then there’s no way of specifying “const reference to the value type of an iterator” without TMP, because iterator_traits is the only means to access the value type of an arbitrary iterator.

    If you want to const-ify the decltype then how about this?

    template<typename Iterator>
    void solve_world_hunger(Iterator it)
    {
        const auto ret_type = *it;
        auto lambda = [](decltype(ret_type)& x){
            auto y = x;   // this should work
            x = x;        // this should fail
        };
    }
    

    You might have to capture ret_type in order to use its type, I can’t easily check at the moment.

    Unfortunately it dereferences the iterator an extra time. You could probably write some clever code to avoid that, but the clever code would end up being an alternative version of remove_reference, hence TMP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I get an element that has a <BR \> inside, and get it's
I'm trying to create an algorithm that set some relevance to a webpage based
I am looking for an algorithm to generate equally distributed points inside a polygon.
Inside my Controller i have function that runs after user clicks on item, which
Inside my .aspx I have some JSON code that looks like this: function someFunctionName()
Inside the initialize() function there is a jQuery each loop. Inside that loop is
I have made small algorithm and want to implement it using javascript. Here is
I have 2 database tables named Categories and SubCategories and I want to create
Basically I want an arbitrarily large stack. I know that's not possible, but could
Just want to ensure you that it is not a homework question. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.