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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:26:08+00:00 2026-05-28T04:26:08+00:00

The following doesn’t compile, how can I do this? I think the example shows

  • 0

The following doesn’t compile, how can I do this? I think the example shows my intent, but I’ll try to add a blurb if people are confused.

template<typename T>
class A
{
private:
    struct B
    {
        template<typename T2> 
        B& operator=( const A<T2>::B& right ){} //  how can I make this work?
    };

    template<typename T2> friend class A;
    template<typename T2> friend class A<T2>::B;
};
  • 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-05-28T04:26:09+00:00Added an answer on May 28, 2026 at 4:26 am

    The fundamental idea of your assignment operator is flawed, as even with the addition of a typename, it’s a non-deducible context. As such, the template parameter will never be deduced and the assignment operator will never work unless you explicitly specify a type like B.operator=<some_type>(other_B).

    An easier version would be to just make it a normal function template, and SFINAE your way out.

    #include <type_traits>
    
    template<class> struct is_a_B;
    
    template<class B2>
    typename std::enable_if<
      is_a_B<B2>::value,
      B&
    >::type operator=(B2 const& other){
      // ...
    }
    

    Now all that’s left is the is_a_B type trait. You can make this easy on yourself with possible false positives:

    template<class B>
    struct is_a_B{
      typedef char yes;
      typedef yes (&no)[2];
    
      template<class T>
      static yes test(typename T::I_am_a_B_type*);
      template<class T>
      static no  test(...);
    
      static bool const value = sizeof(test<B>(0)) == sizeof(yes);
    };
    

    Just provide the I_am_a_B_type typedef in your B class.

    Live example on Ideone. Comment out the b1 = 5; line and it compiles as seen here.


    And now for the slightly more perverted complicated way with no false-positives. 🙂

    template<bool Cond, class OnTrue>
    struct and_v{
      static bool const value = OnTrue::value;
    };
    
    template<class OnTrue>
    struct and_v<false, OnTrue>{
      static bool const value = false;
    };
    
    template<class B>
    struct is_a_B{
      typedef char yes;
      typedef yes (&no)[2];
    
      template<class T>
      static yes has_parent(typename T::parent*);
      template<class T>
      static no  has_parent(...);
    
      template<class T>
      static yes is_A(A<T>*);
      static no  is_A(...);
    
      template<class T>
      struct lazy_test{
        typedef typename std::add_pointer<typename T::parent>::type p_type;
        static bool const value = sizeof(is_A(p_type(0))) == sizeof(yes);
      };
    
      static bool const value = and_v<sizeof(has_parent<B>(0)) == sizeof(yes),
                                      lazy_test<B>>::value;
    };
    

    For this one you need a typedef A<T> parent; inside B. It’s staged in two parts:

    • First I test if a parent typedef exists, and if it does
    • If it’s actually a typedef of the A class template.

    Sadly, the logical operators (&&, ||, ?:) don’t short-circuit in template code like I hoped, so I had to write those and_v templates + a lazy tester that only gets evaluated if a parent typedef exists.

    Live example on Ideone. Again, comment out the b1 = 5; line to make it compile as seen here.

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

Sidebar

Related Questions

The following doesn't compile, but how can I get the equivalent functionality of [MYDIR]
The following doesn't work, but something like this is what I'm looking for. select
I'm trying to use STL, but the following doesn't compile. main.cpp : #include <set>
Can someone explain why the following doesn't compile? I want that BB[A] is also
The following doesn't compile. Do I need to cast the person first? object People
The following doesn't compile public static T Retrieve<T>(this NameValueCollection collection, String key) where T
Can anyone explain why the following doesn't compile? byte b = 255 << 1
For some reason the following doesn't crash like my program does, but I'm pretty
So, I understand that the following doesn't work, but why doesn't it work? interface
following code doesn't work with input: 2 7 add Elly 0888424242 add Elly 0883666666

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.