为浮动类型重载运算符% [英] Overloading operator% for floating types

查看:151
本文介绍了为浮动类型重载运算符%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  float a = 

我试图重载操作符%,因为您不能在double类型上使用模数, 5.0;
float b = 5.0;
a = a%b;
//不允许

我试图用这种函数重载运算符% :

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

其他操作不涉及float我使用:

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

它从来没有编译实际上我卡住了,无法找到一种方法来绕过这个问题,
g ++仍然警告我,你不能在浮动模上执行模,用我的模板语法是错误的
或这真的不可能。

解决方案

您无法按照您希望的方式为原始类型重载操作符。



对于C ++ 11草案n3290,§13.5运算符重载,第6点:


运算符函数应该是非静态成员函数或者是非成员函数,并且至少有一个参数的类型是类,对类的引用,枚举或对枚举的引用。 [b]

原始类型不是类(或枚举),所以它们不能具有成员函数。你不能创建一个全局的 float操作符%(float&,float&),因为它不涉及参数列表中的类或枚举。 (另见 C ++ FAQ 26.10我可以定义一个运算符重载与内置/内部/原始类型一起使用?)。

您至少需要 Float ,并定义你的任何操作。

想要它,但是如果 a 和<$都不能获得 a = a%b; 来使用你的函数c $ c> b 是 float s。



或者你可以 #include > 并使用 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;
返回0;





$ b

自定义浮动包装的简单示例(不完整,可能不太安全按原样,但可以让你开始):

  #include< iostream> 
#include< cmath>

类Float {
private:
float val;
public:
Float(float f):val(f){};

浮点运算符%(float const& other)const {
return std :: fmod(val,other.val);
}
浮点运算符%(float const& other)const {
return std :: fmod(val,other);
}
//转换运算符可能很方便
运算符float(){return val; }
};

int main()
{
Float a = 13.0f;
浮点数b = 5.0f;
浮点数c = a%b;
std :: cout<< c<的std :: ENDL;
//这也可以工作
浮点数d = 13.0f;
float e = 5.0f;
float f = d%e;
std :: cout<< f<的std :: ENDL;
返回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.

解决方案

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;
}

这篇关于为浮动类型重载运算符%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆