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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:19:09+00:00 2026-06-10T04:19:09+00:00

I’m currently implementing some CRTP with a base class template<class CRTP> Base and derived

  • 0

I’m currently implementing some CRTP with a base class template<class CRTP> Base and derived classes Derived1 : public Base<Derived1>, Derived2 : public Base<Derived2>…

The mathematical operators are defined in Base and are of the type CRTP Base<CRTP>::operator+(const CRTP& rhs) that means that we can add a Derived1 to a Derived1 but not a Derived2 to a Derived1.

Furthermore, I have defined the operator Base<CRTP>& Base<CRTP>::operator()() which means that Derived1() will return Base<Derived1>&.

I wonder if there is a solution to do the following :

Derived1 = Derived1 + Derived1 : OK
Derived2 = Derived2 + Derived2 : OK
Derived1 = Derived1 + Derived2 : NOT OK
Derived1 = Derived1() + Derived2() : OK

According to the last two lines :

  • I prevent the user to make a mistake
  • But if he really wants to do this operation he can “cast” the derived types to the base type and it will work

The only thing I need to do this is to define an operator like that :

template<class CRTP0, class = typename std::enable_if</* SOMETHING */>::type> 
Base<CRTP> Base<CRTP>::operator+(const Base<CRTP0>& rhs)

In the enable_if I would like something that is :

  • true : if rhs is of the Base type
  • false : if rhs is a Derived type

Does a such thing exist ? Do you have an other solution in mind ?

Thank you very much !

  • 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-10T04:19:11+00:00Added an answer on June 10, 2026 at 4:19 am

    The /* SOMETHING */ can be easily archived using

    1. std::is_same for the ‘false’ part of Derived and
    2. A helper class for the ‘true’ part of Base

    The helper class is to determine whether a class is exactly a Base<?>:

    template <typename> struct IsBase : std::false_type {};
    ...
    template <typename X> struct IsBase<Base<X>> : std::true_type {};
    

    and then we could fill in that /* SOMETHING */ with:

    std::is_same<Other, Self>::value || IsBase<Other>::value
    

    Note that this allows Derived1 + Derived2().


    Example: http://ideone.com/OGt0Q

    #include <type_traits>
    
    template <typename> struct IsBase : std::false_type {};
    
    template <typename Self>
    struct Base {
        Base& operator()() {
            return *this;
        };
    
        template <typename Other,
                  typename = typename std::enable_if<std::is_same<Other, Self>::value
                                                  || IsBase<Other>::value>::type>
        Self operator+(const Other& other) const {
            return static_cast<const Self&>(*this);
        }
    };
    
    template <typename X> struct IsBase<Base<X>> : std::true_type {};
    
    
    struct D1 : Base<D1> {};
    struct D2 : Base<D2> {};
    
    
    int main() {
        D1 d1;
        D2 d2;
        d1 + d1; // ok
        d2 + d2; // ok
        d1() + d2(); // ok
        d1 + d2; // error
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I am currently running into a problem where an element is coming back from
I have some data like this: 1 2 3 4 5 9 2 6

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.