简体   繁体   中英

How to make C99 math functions available in -std=gnu++98 mode with GCC 4.3?

I want to use isfinite function in my C++ code.

This function is available in the default math.h but not in the default version(-std=gnu++98) of cmath .

So if I include math.h and make sure cmath is not included, then isfinite is available.

If any of other header files, like valarray includes cmath , then isfinite is gone.

C++11 in GCC 4.3 is experimental so I don't want to turn it on.

Is there a way to use C99 math.h in C++98 code?

I found this related question on testing NaN , and the non-C++11 solutions seems very ugly.

EDIT

As is pointed by @old_mountain in the comment, when cmath is used, isfinite is still available but need to be called by std::isfinite , using the std namespace.

Include <cmath> and use std::isfinite with std namespace.

It should work fine (g++4.3.6)

Create the function yourself! According to cppreference that function is available as part of C++11 standard, so I'm not sure is portable using std::isfinite with GCC 4.3.X

isfinite.cpp

#include <math.h>

bool myIsFinite(float arg){
    return isfinite(arg)!=0;
}


bool myIsFinite(double arg){
    return isfinite(arg)!=0;
}

isfinite.hpp

bool myIsFinite(float arg);
bool myIsFinite(double arg);


If you still want to call a function called "isfinite" (I do not suggest that):

isfinite.cpp

bool myIsFinite(float arg);
bool myIsFinite(double arg);

bool isfinite(float arg){
    return myIsFinite(arg);
}


bool isfinite(double arg){
    return myIsFinite(arg);
}

#include <math.h>

bool myIsFinite(float arg){
    return isfinite(arg)!=0;
}


bool myIsFinite(double arg){
    return isfinite(arg)!=0;
}

isfinite.hpp

bool isfinite(float arg);
bool isfinite(double arg);

WARNING

You will not be able to do things like "single translation unit" using this file. So you have to exclude "isfinite.cpp" from any single compilation unit and compile it apart alone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM