How to compile Hinnant's short_alloc allocator - c++

I'd like to try the new Hinnant's short_alloc allocator that, as far as I can understand, replaces the old stack_alloc allocator. However, I can't manage to compile the vector example. g++ says:
~# g++ -std=c++11 stack-allocator-test.cpp -o stack-allocator-test
In file included from stack-allocator-test.cpp:6:0:
short_alloc.h:11:13: error: ‘alignment’ is not a type
short_alloc.h:11:22: error: ISO C++ forbids declaration of ‘alignas’ with no type [-fpermissive]
short_alloc.h:11:22: error: expected ‘;’ at end of member declaration
As far as I can tell, g++ complains about line 10 and 11:
static const std::size_t alignment = 16;
alignas(alignment) char buf_[N];
It seems that the compiler doesn't like the "expression version" of alignas but it expects just the "type-id version".
I'm using g++ 4.7.2 under Ubuntu 12.10.
~# g++ --version
g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Probably I'm missing something obvious, but I can't figure it out. Any help would be appreciated. (Please don't tell me I have to upgrade to a newer g++, I'm too lazy to do that :)

g++-4.7.2 doesn't support alignas. From http://gcc.gnu.org/projects/cxx0x.html:
Alignment support | N2341 | GCC 4.8
Try using g++-4.8.0 or clang; alternatively you may be able to use the __attribute__((aligned)):
__attribute__((aligned (8))) char buf_[12];
Note that __attribute__((aligned)) only accepts certain integer constant expressions (literals, template parameters); it doesn't accept static const variables.

Related

Weird issues with Clang and C++, and uniform initialization

I was attempting to compile and run the example on this page that explores function pointers as a function input. The example I was running was the 66 line one about halfway down the page.
https://www.learncpp.com/cpp-tutorial/function-pointers/
I am on Mac iOS 12.3.1. I tried to compile with
g++ sort.cc
and was getting errors that no semicolons were in my for loops, i believe due to the bracket initialization throughout the code. And when I run it with:
g++ -std=c++11 sort.cc
It works fine.
BUT
Shouldn't my clang be compiling at at least C++11? running
clang -v
I get
Apple clang version 13.1.6 (clang-1316.0.21.2)
Target: x86_64-apple-darwin21.4.0
And from what I can tell clang versions past 4 default to c++14.
Also, when I run using clang or gcc I get errors setting -std=c++xx, but it works fine with g++. But as far as I can tell, g++ and gcc are aliases to clang, and running gcc -v or g++ -v gives me clang version 13.1.6.
So whats going on?
Xcode clang defaults to C++98. Compiling a simple program which has C++11 or later features will tell you that C++11 or later isn't the default. e.g.,
// a.cpp
constexpr int i = 10;
clang a.cpp -c
a.cpp:1:1: error: unknown type name 'constexpr'
constexpr int i = 10;
^
1 error generated.
while clang a.cpp -c -std=c++11 works fine.
Also see: https://trac.macports.org/wiki/CompilerSelection

Compiling c++14 Code with Mac Terminal Compiler

I'm trying to code the BST ADT, and the specification we were given requires use of 'auto' that is only included in C++14. I'm trying to compile, but I keep getting errors that 'auto' is only included in C++14, so I'm just wondering if there's a different way to compile the code so that it includes C++14? In every previous project I've done (over the last three semesters) I've been able to compile the file (say called main.cpp) just by using the code:
g++ -o main main.cpp
I've tried the following compile code
g++ -std=c++14 -o main main.cpp
but when I do that, I get like 100 errors that look like
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:1002:86: error: member reference base type 'std::__1::basic_string::__self_view' (aka 'int') is not a structure or union
append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:1002:99: error: member reference base type 'std::__1::basic_string::__self_view' (aka 'int') is not a structure or union
append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
What do these even mean?
I figured out the reasons behind the errors (turned out to be templating mistakes), but in the end compiling with g++ -std=c++14 -o main main.cpp is working. I still don't really understand any of the compilation stuff though, so if someone could explain a little bit/ provide a link I would appreciate
(this is not an answer, but too long for a comment)
Where did you get your g++ from? On most modern Mac OS installs, it is a symlink for clang++. What does g++ --version print?
Certainly you're using libc++, not libstdc++. The __1 in the type names is a giveaway, along with the path to <string>.
And the error message ('std::__1::basic_string::__self_view' (aka 'int') is weird, too. basic_string::__self_view is a string_view, not an int.
if you use Unix based operating systems such as Linux and OS X try this:
clang++ -g -std=c++1y main.cpp -o main

-std=c++ 98 and OS X 10.10

I'm currently trying to compiling my program with the -std=c++98 flag on OS X 10.10:
clang++ -std=c++98 -pedantic -W -Wall -Werror *.cpp
g++ -std=c++98 -pedantic -W -Wall -Werror *.cpp
Strangely when I compile with OS 10.10 no error are showed while some are showed with a GNU/Linux distribution.
With the GNU/Linux distribution I have some errors because I use s.open(file); instead of s.open(file.c_str()); but on OS X no error, even by using s.open(file);.
Maybe there is just a link between this error and the filesystem of each OS?
You're correct. This code shouldn't compile under C++98:
#include <fstream>
#include <string>
int main() {
std::string file("/tmp/foo.txt");
std::ifstream s;
s.open(file);
}
It's technically a bug in libc++, but it's one that I doubt that they will want to fix.
If you wanted, you could compile with libstdc++ as the standard library, which doesn't have this feature implemented (at least the version that apple distributes doesn't).
[10:13am][wlynch#watermelon /tmp] clang++ -std=c++98 -stdlib=libstdc++ foo.cc
foo.cc:7:12: error: no viable conversion from 'std::string' (aka 'basic_string<char>') to 'const char *'
s.open(file);
^~~~
/usr/include/c++/4.2.1/fstream:518:24: note: passing argument to parameter '__s' here
open(const char* __s, ios_base::openmode __mode = ios_base::in)
^
1 error generated.
The problem is that on OS X you're still using a C++11 implementation of the standard library, which includes such API changes as the ostream::open method accepting std::strings.
Changing the C++ standard with -std=c++98 does not affect the standard library being used, and libc++ does not implement C++98 (e.g., there's no #ifdef to remove those open(std::string) APIs when in C++98 mode) whereas I think libstdc++ does hide non-c++98 APIs when building in C++98 mode.

Where is definition of std::function in clang++ (3.3/Xcode)

Problem Solved => see the update at the end
I'm trying to use std::function but it looks like just include <functional> does not provide the definition. I have tried to compile following code:
#include <functional>
std::function<int(int)> f = nullptr;
with c++11 as compile option:
% clang++ -c -std=c++11 t.cc
cause:
t.cc:3:6: error: no type named 'function' in namespace 'std'
std::function<int(int)> f = nullptr;
~~~~~^
t.cc:3:14: error: expected unqualified-id
std::function<int(int)> f = nullptr;
^
2 errors generated.
what am I missing? I know C++ well but new to clang++/C++11 thus I lack of important knowledge, I guess.
I'm using clang++ on MacOS X 10.8.
Update 1
I have tried a sample at cppreference.com site but it won't compile too. Giving some option solve the problem?
Update 2
Tried above sample from cppreference.com with clang++ -c -std=c++11 -stdlib=libc++11 x.cc, and compiler still says:
x.cc:1:10: fatal error: 'functional' file not found
#include <functional>
^
1 error generated.
Where is functional? I guess I should give -stdlib=libc++11 or whatever but it does not work too:
clang: error: invalid library name in argument '-stdlib=libc++11'
How I can find list of argument for -stdlib? (note: in man page, only available options are libc++ and libstdc++ both of them don't work)
Or functional just does not work?
This is not about the definition of the function. You don't have a linker error. You have a compiler error. The problem is, presumably, that the BSD/GNU/Darwin standard library installed in the real sysroot doesn't support C++11. You have to use the one that comes with Clang by specifying the -stdlib=libc++ compiler flag.
For C++11, it's best to always invoke clang as: clang++ -std=c++11 -stdlib=libc++
I use this most of the time, so I set the environment variable $CXX to this value. That way, I'm getting the dialect and library option in both compilation and linking. -std=c++11 is insufficient, as clang will still use the (old) system gcc headers in /usr/include/c++/4.2.1.
-stdlib=libc++ will use the clang headers in /usr/lib/c++/v1 such as <functional>.
There's a similar question with an answer by Howard Hinnant, who is (IIRC) an Apple engineer.

c++11: clang refuses numeric_limits<> in my template definition, while gcc accepts it - which is right?

The offending code:
template <class Bar,
size_t MAX_SIZE = std::numeric_limits<size_t>::max()>
size_t foo(Bar const& b) { omitted... }
It compiles fine on gcc 4.7.2 with -std=c++11. On clang 3.0 I get the following error:
foo.hpp:35:28: error: non-type template argument of type 'unsigned long' is not an integral constant expression
size_t MAX_SIZE = std::numeric_limits<size_t>::max()>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As far as I can tell, I am supposed to be able to use numeric_limits in this way in c++11. Is clang wrong here, or am I unaware of something?
EDIT:
Compilation flags are: clang++ -o foo.o -c -W -Wall -Wextra -Werror -std=c++11 -stdlib=libc++ -g -I. foo.cpp
Your code compiles just fine with clang++ 3.2, see here.
I would say there is nothing wrong with your code but you should upgrade to a newer version of clang.
Note: The code doesn't compile with the Intel C++ Compiler 13.0.1 due to a compiler bug (thanks #Xeo):
Compilation finished with errors:
source.cpp(6): internal error: assertion failed: ensure_il_scope_exists: NULL IL scope (shared/cfe/edgcpfe/il.c, line 7439)
size_t MAX_SIZE = std::numeric_limits<size_t>::max()>
^
compilation aborted for source.cpp (code 4)
To use C++11 library features with clang you need to use the libc++ standard library implementation, otherwise you get the ancient library from GCC 4.1.2, which doesn't support C++11
See https://stackoverflow.com/a/14790442/981959 and https://stackoverflow.com/a/14150421/981959 and many other questions.