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

  • Home
  • SEARCH
  • 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 7858391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:24:50+00:00 2026-06-02T21:24:50+00:00

Lets say I have a class with a member variable: std::unordered_map<KeyType, std::shared_ptr<ValueType>> myMap and

  • 0

Lets say I have a class with a member variable:

std::unordered_map<KeyType, std::shared_ptr<ValueType>> myMap

and in a member function I want to do the following:

std::for_each(myMap.begin(), myMap.end(), [](std::pair<const KeyType, std::shared_ptr<ValueType>>& pair){pair.second->someMethod(); });

Is there anyway to shorten the lambda expression? I thought I could do this but it was not valid syntax:

std::for_each(myMap.begin(), myMap.end(), [](decltype(myMap::valueType)& pair){pair.second->someMethod(); });
  • 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-02T21:25:02+00:00Added an answer on June 2, 2026 at 9:25 pm

    I recomend typedefing complex templates like the assoc containers, for this reason so you could do something like:

    typedef std::unordered_map<KeyType, std::shared_ptr<ValueType>> map_type;
    
    map_type myMap;
    
    //do with map
    
    std::for_each(myMap.begin(), myMap.end(), 
        [](typename map_type::value_type& pair){
            pair.second->someMethod(); 
    });
    

    or without the typedef

    std::for_each(myMap.begin(), myMap.end(), 
        [](typename decltype(myMap)::value_type& pair){
            pair.second->someMethod(); 
    });
    

    decltype gets the type of an object, you need to use the typename defined in a templated class, to do this you use the typename keyword. This is necessary in case a template specialisation doesn’t have that typedef.

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

Sidebar

Related Questions

let's say i want to have a member variable for a pointer to std::vector
Lets say I have: class X { function a() { echo Hello, ; }
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }
Let's say we have a template class Area , which has a member variable
Let's say i have a class Foo and it has a static member variable
Let's say I have a class that has a member called data which is
lets say i have a class defined as shown below. Lets now say i
Lets say I have a concrete class Class1 and I am creating an anonymous
Lets say we have a derivided class SerializableLabel from the base class System.Windows.Controls. [XmlRoot(SerializableLabel)]
Lets say have this immutable record type: public class Record { public Record(int x,

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.