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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:41:35+00:00 2026-05-29T08:41:35+00:00

I’m trying to overload the operator % because you can’t use modulus on double

  • 0

I’m trying to overload the operator % because you can’t use modulus on double types,

float a = 5.0; 
float b = 5.0;
a  = a % b;
// not allowed

I Was trying to overload the operator % with this kind of function :

template <>
MyClass*                MyClass<float>::operator%(Myclass &other)

For other operation non involving float I use :

template <class T>
MyClass*                MyClass<T>::operator%(MyClass &other)

It never compiled actually I’m stuck and can’t find a way to bypass this problem,
g++ is still warning me that you can’t perform modulo on floats, is something wrong
with my template syntax or is it really impossible.

  • 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-29T08:41:36+00:00Added an answer on May 29, 2026 at 8:41 am

    You can’t overload operators for primitive types the way you’d want it to work.

    For C++11 draft n3290, §13.5 Operator Overloads, point 6:

    An operator function shall either be a non-static member function or be a non-member function and have at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration. […]

    Primitive types aren’t classes (or enums), so they can’t have member functions. And you can’t create a global float operator%(float&,float&) since that doesn’t involve a class or enum in the parameter list. (See also C++FAQ 26.10 “Can I define an operator overload that works with built-in / intrinsic / primitive types?”.)
    You need at least one of the terms in the % expression to be a user-defined type.

    You could create a class Float and define whatever operations you want on it, but you cannot get a = a % b; to use your function if both a and b are floats.

    Or you could #include <cmath> and use std::fmod:

    #include <iostream>
    #include <cmath>
    
    int main()
    {
        float a = 13.0f;
        float b = 5.0f;
        a  = std::fmod(a, b);
        std::cout << a << std::endl;
        return 0;
    }
    

    Simple example with a custom “float wrapper” (incomplete, probably not quite safe as-is, but can get you started):

    #include <iostream>
    #include <cmath>
    
    class Float {
        private:
            float val;
        public:
            Float(float f): val(f) {};
    
            Float operator%(Float const& other) const {
                return std::fmod(val, other.val);
            }
            Float operator%(float const& other) const {
                return std::fmod(val, other);
            }
            // conversion operator could be handy
            operator float() { return val; }
    };
    
    int main()
    {
        Float a = 13.0f;
        Float b = 5.0f;
        Float c  = a % b;
        std::cout << c << std::endl;
        // this also works
        Float d = 13.0f;
        float e = 5.0f;
        float f  = d % e;
        std::cout << f << std::endl;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.