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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:12:56+00:00 2026-06-12T22:12:56+00:00

I tried googling this with no luck, so I’m trying here. I have a

  • 0

I tried googling this with no luck, so I’m trying here.

I have a couple of classes, each of which defines a member struct foo. This member type foo can itself inherit from one of the former classes, hence obtaining a member type foo itself.

I want to access the nested foo types using template metaprogramming (see below), but c++ name injection introduces problems, as the upper foo type name gets injected into the lower foo type, and the upper one gets resolved when I want to access the lower, say using A::foo::foo.

Here is an example:

#include <type_traits>

struct A;
struct B;

struct A {
    struct foo;
};

struct B {
    struct foo;
};

struct A::foo : B { };
struct B::foo : A { };

// handy c++11 shorthand
template<class T>
using foo = typename T::foo;

static_assert( std::is_same< foo< foo< A > >, foo< B > >::value, 
               "this should not fail (but it does)" );

static_assert( std::is_same< foo< foo< A > >, foo< A > >::value, 
               "this should fail (but it does not)" );

FYI, I’m implementing function derivatives, foo is the derivative type. The above situation happens e.g. with sin/cos.

TLDR: how do i get foo<foo<A>> to be foo<B>, not foo<A> ?

Thanks !

  • 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-12T22:12:57+00:00Added an answer on June 12, 2026 at 10:12 pm

    This is not really an automatic solution but solves the problem. Your
    types provide a typedef to the base class, absence/presence of this
    typedef is detected through SFINAE and the nested foo is found either
    through the base or through normal look-up.

    You can probably automate the has_base to check a list of known
    bases with is_base_of if you need more automation.

    #include <type_traits>
    template <typename T>
    struct has_base
    {
        typedef char yes[1];
        typedef char no[2];
    
        template <typename C>
        static yes& test(typename C::base*);
    
        template <typename>
        static no& test(...);
    
        static const bool value = sizeof(test<T>(0)) == sizeof(yes);
    };
    
    struct A {
        struct foo;
    };
    
    struct B {
        struct foo;
    };
    
    struct A::foo : B { typedef B base; };
    struct B::foo : A { typedef A base; };
    
    template<typename T, bool from_base = has_base<T>::value >
    struct foo_impl {
      typedef typename T::base::foo type;
    };
    
    template<typename T> 
    struct foo_impl<T, false> {
      typedef typename T::foo type;
    };
    
    template<typename T>
    using foo = typename foo_impl<T>::type;
    
    static_assert( std::is_same< foo< foo<A> >::, foo< B > >::value, 
                   "this should not fail (but it does)" );
    
    static_assert( std::is_same< foo< foo< A > >, foo< A > >::value, 
                   "this should fail (but it does not)" );
    int main()
    {
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a dumb question, I have tried googling but I am probably
I tried googling woht no luck, maybe someone encountered this I'm using MinGW on
I guess the topic says it. I have tried googling this, but havent gotten
I've tried googling this and I cant seem to find a solution/script. I want
I've tried Googling and searching stackoverflow, but have yet to find a solution too
tried to find the answer by googling and in MSDN but with no luck.
No luck in googling on this error message features/manage_hand_evaluator.feature: Parse error at features/manage_hand_evaluator.feature:21. Found
So I have been trying for hours to get this to work and I
I tried googling this but can't find anything. I'm just curious as to why
I've spent a great long while googling this problem without any luck and I've

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.