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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:56:54+00:00 2026-05-30T19:56:54+00:00

Suppose that I have following class template<unsigned char B, unsigned char F> class Foo

  • 0

Suppose that I have following class

template<unsigned char B, unsigned char F>
class Foo
{
   .....
}

I hope to overload the operator+ so that if the two inputs are of

Foo<B1, F1> 

and

Foo<B2, F2>, 

respectively, I would like the return value is type of

Foo<max(B1, B2), max(F1, F2)>. 

or something like

Foo<max(B1-F1, B2-F2)+max(F1, F2), max(F1, F2)>. 

Any tips?

  • 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-30T19:56:55+00:00Added an answer on May 30, 2026 at 7:56 pm

    Just compute your new type in the operator+ return type, and use MPL for comparisons
    Moreover, you don’t need friend for that neither your operator+ need to be mutable.

    Simple example with simple classes:

    #include <boost/mpl/max.hpp>
    
    template<unsigned char B, unsigned char F>
    class Foo
    {};
    
    template< unsigned char B1, unsigned char F1
            , unsigned char B2, unsigned char F2
            >
    Foo< boost::mpl::max_<boost::mpl_::char_<B1>, boost::mpl_::char_<B2> >::value
       , boost::mpl::max_<boost::mpl_::char_<F1>, boost::mpl_::char_<F2> >::value
       >
    operator+(Foo<B1,F1> const& a, Foo<B2,F2> const& b)
    {
      Foo< boost::mpl::max_<boost::mpl_::char_<B1>, boost::mpl_::char_<B2> >::value
       , boost::mpl::max_<boost::mpl_::char_<F1>, boost::mpl_::char_<F2> >::value
       > that;
      return that;
    }
    

    Now, notice how it is cumbersome. An usual idiom in this case is to ue Boost MPL Integral Types instead of raw value whenever possible

    #include <boost/mpl/max.hpp>
    
    template<class B, class F>
    class Foo
    {};
    
    template< class B1, class F1
            , class B2, class F2
            >
    Foo< typename boost::mpl::max_<B1, B2>::type
       , typename boost::mpl::max_<F1, F2>::type
       >
    operator+(Foo<B1,F1> const& a, Foo<B2,F2> const& b)
    {
       Foo< typename boost::mpl::max_<B1, B2>::type
          , typename boost::mpl::max_<F1, F2>::type
          > that;
      return that;
    }
    
    Foo< boost::mpl::int_<4>, boost::mpl::int_<8> > x;
    

    EDIT:
    Also, this makes the return type of + a bit complex to write down in pre_C++11 auto.
    Another classical stuff is to make this into a function object following the result_of protocol so you have a meta-function handy to compute the return type in an opaque way.

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

Sidebar

Related Questions

Suppose I have following template template<unsigned char I, unsigned char F> class FOO {
Suppose I have the following class Foo, that supports a function of any arity
Suppose that I have the following mapping with a formula property: <class name=Planet table=planets>
Suppose I have the following: #include <iostream> #include <string> template<class T> class base {
The scenario Suppose I have the following two model classes: public class ProductColor {
Suppose I have template< unsigned int num > class SomeFunctionality { static unsigned int
Suppose that I have the following HBM mapping: <class name=Student table=student> <set name=classes table=student_classes
Let's suppose that I have the following class which tries to be immutable public
Suppose that I have the following python base class: class BaseClass(object): def a(): This
Lets suppose that I have the following simple query var q = from p

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.