0

According to clang's C++-1z status page, I believe 4.0 should support C++17 std::optional.

However I'm having trouble getting it to work. See this simple example:

#include <optional>

int main() {
    return 0;
}

Trying to compile it in various ways, all fail:

$ clang++-4.0 main.cpp
In file included from main.cpp:1:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.1.0/../../../../include/c++/7.1.0/optional:33:
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.1.0/../../../../include/c++/7.1.0/bits/c++17_warning.h:32:2: error: This file
      requires compiler and library support for the ISO C++ 2017 standard. This support must be enabled with the
      -std=c++17 or -std=gnu++17 compiler options.
#error This file requires compiler and library support \
 ^
1 error generated.

(this alone is a bit weird as the error mentions adding -std=c++17 but it turns out the flag's name is c++1z and there is no c++17:)

$ clang++-4.0 -std=c++17 main.cpp
error: invalid value 'c++17' in '-std=c++17'

Anyway let's proceed:

$ clang++-4.0 -std=c++1z main.cpp
In file included from main.cpp:1:
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.1.0/../../../../include/c++/7.1.0/optional:1032:27: error: use of class template
  'optional' requires template arguments
  template <typename _Tp> optional(_Tp) -> optional<_Tp>;
                      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.1.0/../../../../include/c++/7.1.0/optional:451:11: note: template is declared
  here
class optional
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.1.0/../../../../include/c++/7.1.0/optional:1032:40: error: expected ';' at end of
  declaration
  template <typename _Tp> optional(_Tp) -> optional<_Tp>;
                                   ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.1.0/../../../../include/c++/7.1.0/optional:1032:41: error: cannot use arrow
  operator on a type
  template <typename _Tp> optional(_Tp) -> optional<_Tp>;
                                    ^
3 errors generated.

Last try:

$ clang++-4.0 -std=c++1z -stdlib=libc++ main.cpp
main.cpp:1:10: fatal error: 'optional' file not found
#include <optional>
         ^~~~~~~~~~
1 error generated.

clang info:

$ clang++-4.0 --version
clang version 4.0.1-svn305187-1~exp1 (branches/release_40)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Am I doing something wrong?

Ela782
  • 5,041
  • 5
  • 53
  • 66
  • Do you have the 4.0 version of libc++? If so, try adding it explicitly as include – JVApen Jul 07 '17 at 17:56
  • @JVApen How would I do so? I.e. which package from the `apt-get install` list from http://apt.llvm.org/ do I need? (there's none containing anything like "libc++") – Ela782 Jul 07 '17 at 17:57
  • https://stackoverflow.com/q/44262236/2069064 – Barry Jul 07 '17 at 17:58
  • You can checkout the code as described at http://libcxx.llvm.org/docs/BuildingLibcxx.html – JVApen Jul 07 '17 at 18:01
  • @JVApen I am not going to build libcxx - it needs to work for customers via apt repositories. Anyway seems like there is no solution but to use gcc-7 or clang-5 (SVN). – Ela782 Jul 07 '17 at 18:09
  • If you need optional support in C++11/C++14 in the meantime, the author of Boost.Optional wrote a standalone version that is quite good. It's a single header and so you can check the `__cplusplus` version and include it or the STL version: https://github.com/akrzemi1/Optional – Alex Huszagh Jul 07 '17 at 18:21
  • 1
    @AlexanderHuszagh I know about it, thanks! I was hoping that compilers are far enough now and I wouldn't need that anymore in comparison to a few months ago. :-) I may have to reconsider. It's a bit a shame that akrzemi1 optional is defined in the std::experimental namespace so there's some include/ifdef headache with compilers that have it in std:: already. – Ela782 Jul 07 '17 at 18:35

0 Answers0