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

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

Related

Alternative to std::default_random_engine for Xcode?

I'm trying to compile a program I wrote in linux on Xcode and for my random functions I used:
std::random_device seed_device;
std::default_random_engine engine(seed_device());
But default_random_engine won't compile on Xcode. Is there a different version I should use?
You need to #include <random>.
In general, when the compiler complains about "no type named function_name in namespace std", you have to check that:
You have included the right header. You can look up the name of the right header on any decent C++ reference.
Your standard library supports C++1x (*) (or, at least, the feature you want).
Your compiler supports C++1x (*) (or, at least, the feature you want), and the right flags are used to enable it, if required.
(*) The x stands for the version of the C++ standard that includes the feature you want.

cstdint in C++ is not defining uint_t family

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.

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.

No type named 'atomic' in namespace 'std'

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;

why I can't use the function adaptor compose2?

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