why I can't use the function adaptor compose2? - c++

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

Related

Is std::log2() an extension or C++ standard?

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).

How to detect the libstdc++ version in Clang?

I would like to write a "portable" C++ library in Clang. "Portable" means that I detect (in C preprocessor) what C++ features are available in the compilation environment and use these features or provide my workarounds. This is similar to what Boost libraries are doing.
However, the presence of some features depends not on the language, but on the Standard Library implementation. In particular I am interested in:
type traits (which of them are available and with what spelling)
if initializer_list being constexpr.
I find this problematic because Clang by default does not use its own Standard Library implementation: it uses libstdc++. While Clang has predefined preprocessor macros __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__, they are hardcoded to values 4, 2, 1 respectively, and they tell me little about the available libstdc++ features.
How can I check in Clang preprocessor what version of libstdc++ it is using?
Clang does come with its own standard library implementation, it's called libc++. You can use it by adding -stdlib=libc++ to your compile command.
That being said, there are various ways to check Clang/libstdc++ C++ support:
Clang has the __has_feature macro (and friends) that can be used to detect language features and language extenstions.
Libstdc++ has its own version macros, see the documentation. You'll need to include a libstdc++ header to get these defined though.
GCC has its version macros which you already discovered, but those would need to be manually compared to the documentation.
And also, this took me 2 minutes of googling.
This is what I think would help. It prints the value of the _LIBCPP_VERSION macro:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char * argv[])
{
cout<<"Value = "<<_LIBCPP_VERSION<<endl;
return 0;
}
Compile it again the version of clang you want the info for.

fenv.h from C99 in C++11

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.

error: strstream.h: No such file or directory

I am trying to run an old C++ code in Linux (Redhat). I am using gcc version 4.1.2.
I got the following error:
error: strstream.h: No such file or directory
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.cpp:41: error: âostrstreamâ was not declared in this scope
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.cpp:41: error: expected `;' before âstrDestXMLâ
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.cpp:62: error: âstrDestXMLâ was not declared in this scope
This code was running fine under Solaris with gcc version 2.95. The line pointed to by the error contains the following statement:
ostrstream strDestXML;
How do I solve this?
You can #include <strstream> (note absence of the '.h' suffix).
But if you want to properly port the code to modern C++, you should consider changing this to #include <sstream> and std::ostringstream strDestXML; as suggested in the comment.
Standard C++ headers do not have extension.
#include <sstream>
Standard classes are contained in std namespace:
std::ostringstream strDestXML;
Finally, strstream is deprecated; use stringstream instead - that's why I used it here.
And, just a note about GCC version - 4.1.2 is old, no matter what - use something newer.
The modern name for this include is <strstream>. (Although it's formally deprecated, it's still required.) The classes it defines are in namespace std, and have slightly different semantics than the classical iostream, so you may have to do a little bit of modification later anyway. (Depending on how it is being used, it might make sense to change to <sstream>, replacing [io]strstream with std::[io]stringstream.)

How to get headers for unordered_set in gcc v4.1.2?

I'd like to use unordered_set without installing Boost. I tried to add --std=gnu++0x but it is not a recognized option. Does v4.1.2 include unordered_set? If so, how do I get the header file for it?
This is a Centos 4 machine.
unordered_set is in the purview of the standard C++ library, not gcc, the compiler (although most programs built using gcc are linked against libstdc++).
The way you generally include it is #include <tr1/unordered_set>. Then, to use it, you must either do a using std::tr1::unordered_set; or qualify the name each time.
The C++ standard version you choose to use doesn't have much effect because that's the language standard, and the availability of standard library constructs is semi-independent.
IIRC, gcc-4.2 did not have unordered containers at least not in namespace std. I know -std=c++0x was not in place till around gcc-4.3.
Have you tried this:
#include <tr1/unordered_set>
...
std::tr1::unordered_set<int> usint;
...
Notice the tr1/ in the header.
Having said that, gcc-4.1 is pretty old. Any chance you could try say gcc-4.5 or 4.6 and use the std container?