Does fenv.h from C99 exists in C++11? Is there any other ways to use function like fesetround? Maybe boost?
gcc 4.7.2 compiles this code:
#include <cfenv>
int main() {}
http://liveworkspace.org/code/ffbd8e8a24633c7e74f7bcead3b1a287
Does fenv.h from C99 exists in C++11?
Yes it does.
Reference:
26.3 The floating-point environment [cfenv]
Standard C++11 26.3.1 Header <cfenv> synopsis [cfenv.syn]
Is there any other ways to use function like fesetround()? Maybe boost?
The function fesetround() is already provided by cfenv in C++11.
Also, fenv.h also exists in C++11 so as to provide backward compatability to c standard headers. It is covered under:
D.5 C standard library headers [depr.c.headers]
What is the difference between using fenv.h or cfenv?
Including cfenv imports the symbol names in std namespace and possibly in Global namespace.
Including fenv.h imports the symbol names in Global namespace and possibly in std namespace.
Yes, it does. cfenv appears to be the correct header, and it was just added in C++11.
Related
I am now trying to build a c++ library in linux with cmake. If I do not enable -std=c++0x option, I always get compilation errors error: 'div_t' was not declared in this scope for the following codes:
int xPos;
div_t divResult;
divResult = div(xPos,8);
Then if I enable -std-c++0x options with cmake: set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x", then everything is fine. However, in my library I did not use any c++0x features, so I am reluctant to set std=c++0x option. So I search the head file that defines div_t and find it is defined in stdlib.h within the following MACRO:
__BEGIN_NAMESPACE_STD
typedef struct
{
int quot;
int rem;
} div_t;
....
....
__END_NAMESPACE_STD
It seems to me that if I can enable these macros I can build the library without enabling c++0x feature. So my question is what I can do in this situation.
By the way, I can build the library very well without enabling c++0x feature if only g++4.4 is installed in the linux machine. When I also install g++4.6 and make g++4.6 the default g++, then the compilation error began to occur. Even I changed the default g++ to g++4.4, the compilation error still exists if I do not enable c++0x feature.
The macros expand to namespace std { and } respectively if the code is pulled in through a C++ standard library header. This leads me to believe that you're not #including stdlib.h directly (which is good!).
Earlier versions of libstdc++ pulled symbols from C legacy headers into the global namespace even if the C++ versions of these headers (e.g. <cstdlib> instead of <stdlib.h>) were used; newer ones place them only in namespace std.
The cleanest way to fix this is to
#include <cstdlib>
in all translation units where the problem occurs and to use std::div instead of div. If you're lazy, you can also
#include <stdlib.h>
in all translation units that use div, but mixing C and C++ is always icky. Not terribad in this particular case, though.
gcc (4.8.1) and clang (3.4) compile my C++ program which uses std::log2(x). Is this standard compliant?
It is now included in <cmath> header since C++11.
You can find more information here: std::log2
If you are using an earlier C++ compiler that doesn't have this function, you can use log(x) / log(2.0).
I'm building a project in C++ and I'm having some standard library issues.
When I use uint_t family types, the compiler shows an error message.
For example, when I declare a uint_8 type variable, it show an error in vim
uint8_t in namespace std does not name a type
does anyone know the problem?? (of course I included cstdint)
I tried stdint.h of C library and it works.
(though I actually want to use the standard C++ lib)
Ddditionally, the problem is that I have same problems with other c++ standard classes such as array`
And I use g++ version 4.8.2
The support of <cstdint> and <array> is added since C++11, add the flag -std=c++11 to g++, and it'll compile.
Why doesn't
std::atomic<int> index;
Work?
Currently using LLVM 3.1 with these params
C Language Dialect GNU [-std=gnu99]
C++ Language Dialect [-std=c++11]
C++ Standard Library libc++(LLVM C++ standard library with C++11 support)
There are several things that need to be true for your code to work:
You need to #include <atomic>
You need to compile the code as C++11 or C++14 (-std=c++11 or -std=c++14 (or c++0x for older compilers))
Your compiler and standard library needs to support enough of C++11 to provide atomic (http://clang.llvm.org/cxx_status.html)
Adding -std=c++11 to CXXFLAGS in my Makefile -> that works for me!
You need to write it as the following to defined variable.
std::atomic<std::int> index;
I use GCC 4.1.2 on linux, and the STL must be SGI STL.
After I wrote:
#include <functional>
std::find_if(PirateFlush, PirateFlush + size,
compose2(std::logical_and<bool>(), bind2nd(std::greater<UInt32>(), time),
bind2nd(std::less<UInt32>(), now)));
the compiler said:
error: ‘compose2’ was not declared in this scope
what's the matter?
compose2 is not standard and is neither in the global nor std namespaces.
gcc ships some non-standard SGI extensions in the __gnu_cxx namespace.
Use __gnu_cxx::compose2, or boost has many of these predefined.
-nick