cstdint in C++ is not defining uint_t family - c++

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.

Related

how to enable __BEGIN_NAMESPACE_STD in stdlib.h

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.

Xcode 5.1 C++98 char16_t

I need to compile a c++ (98. I cannot migrate yet to 11) code into a mex file.
Unfortunately after upgrading to Xcode 5.1 (which updated also Clang to 3.4 version) I cannot compile the code.
It is the same problem as in here: MEX compile error: unknown type name 'char16_t'
Unfortunately the accepted answer is to compile with c++11 support which I cannot do.
Reading in the source code of Clang I found these lines:
// In C11 these are environment macros. In C++11 they are only defined
// as part of <cuchar>. To prevent breakage when mixing C and C++
// code, define these macros unconditionally. We can define them
// unconditionally, as Clang always uses UTF-16 and UTF-32 for 16-bit
// and 32-bit character literals.
Builder.defineMacro("__STDC_UTF_16__", "1");
Builder.defineMacro("__STDC_UTF_32__", "1");
Now.. I wonder why if they define the macro they do not define the type char16_t.
And also... I cannot include (file not found) either cuchar (C++11) or uchar.h (C11)
Some idea on how to solve this problem?
EDIT: I'd like to understand if this is a bug of Clang (and I have to signal it) or not.
According to C++11 standard (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2018.html) the marco __STDC_UTF_16__ should be defined in the header file cuchar. But I cannot find that file. So because of that I would expect that the macro is undefined. I think the MEX include file relies on this fact.
Just define char16_t (maybe CHAR16_T) as a macro somewhere in your code.
#define char16_t uint16_t
Or, pass -DCHAR16_T=uint16_t flag at compile time.

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;

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?