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 8423209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:39:52+00:00 2026-06-10T03:39:52+00:00

Having a class like this: class A { public: bool hasGrandChild() const; private: bool

  • 0

Having a class like this:

class A {
public:
    bool hasGrandChild() const;

private:
    bool hasChild() const;
    vector<A> children_;
};

Why is it not possible to use a private method hasChild() in a lambda expression defined in the method hasGrandChild() like this?

bool A::hasGrandChild() const {
    return any_of(children_.begin(), children_.end(), [](A const &a) {
        return a.hasChild();
    });
}

Compiler issues an error that the method hasChild() is private within the context. Is there any workaround?

Edit:
It seems that the code as I posted it originally works. I thought that it is equivalent, but the code that does not work on GCC is more like this:

#include <vector>
#include <algorithm>

class Foo;

class BaseA {
protected:
    bool hasChild() const { return !children_.empty(); }
    std::vector<Foo> children_;
};

class BaseB {
protected:
    bool hasChild() const { return false; }
};

class Foo : public BaseA, public BaseB {
public:
  bool hasGrandChild() const {
    return std::any_of(children_.begin(), children_.end(), [](Foo const &foo) {
        return foo.BaseA::hasChild();
      });
  }  
};

int main()
{
  Foo foo;
  foo.hasGrandChild();
  return 0;
}

Seems that there is a problem with fully qualified names as this does not work, but this works.

  • 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-10T03:39:54+00:00Added an answer on June 10, 2026 at 3:39 am

    It seems to be just a GCC bug in a special case when the lambda tries to access a protected member from parent class using fully qualified name. This does not work:

    class Base {
    protected:
        bool hasChild() const { return !childs_.empty(); }
        std::vector<Foo> childs_;
    };
    
    class Foo : public Base {
    public:
      bool hasGrandChild() const {
        return std::any_of(childs_.begin(), childs_.end(), [](Foo const &foo) {
          return foo.Base::hasChild();
        });
      }  
    };
    

    , but this works:

    class Foo : public Base {
    public:
      bool hasGrandChild() const {
        return std::any_of(childs_.begin(), childs_.end(), [](Foo const &foo) {
          return foo.hasChild();
        });
      }  
    };
    

    According to C++11, 5.1.2/3:

    The type of the lambda-expression (which is also the type of the
    closure object) is a unique, unnamed non-union class type — called the
    closure type — whose properties are described below. This class type
    is not an aggregate (8.5.1). The closure type is declared in the
    smallest block scope, class scope, or namespace scope that contains
    the corresponding lambda-expression
    .

    And then C++11, 11.7/1:

    A nested class is a member and as such has the same access rights as
    any other member.

    So the mentioned function-local lambda should have the same access rights as any other member of the class. Therefore it should be able to call a protected method from a parent class.

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

Sidebar

Related Questions

Having a snippet like this: import yaml class User(object): def __init__(self, name, surname): self.name=
Having a program like this: #include <iostream> #include <string> using namespace std; class test
i am having a bean class like public class ItemBean{ String item_id, item_title, image_url,
How do I avoid requiring code like this: public static class BusinessLogicAutomapper { public
I need some help. I am creating a SelectItem class like this: public class
Sometimes it's useful to derive my entities from a base class like this: public
I have something like this: class myClass { protected ICollection<Expression<Func<event_info, bool>>> _whereConditionsList = new
I'm having a class with a list of constants. I want something like get_object_vars
motivation: I would like to create a utility class so that instead of having
Having such class can I instatiate it in place? abstract class Mammal { public

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.