Using C++17 'any' with Xcode 8.1
Asked Answered
S

4

7

I am using C++ in Xcode version 8.1. I need to use the functionality of boost::any but am strongly opposed to pulling any part of Boost into our project (let's not debate it please).

I see that std::any is "merged into C++17" here.

I want to use this in my Xcode 8.1 project. I have tried using -std=c++1z as a custom flag on the project, but I can't seem to find a header for it.

How can I use std::any or std::experimental::any in my Xcode project?

Can I download the appropriate headers from an implementation and throw them into my project's sourcecode? Or, even better, is actually available to now in my version of Xcode/Clang/C++?

Sokotra answered 11/12, 2016 at 20:21 Comment(10)
Xcode is an IDE. It comes with a compiler, but you are not required to use that compiler.Akel
I understand that, but I want to use the vanilla Xcode 8.1 installation and it's default compiler. Xcode is also not a C++ implementation, I understand that too. I want to use the C++ implementations that are available to me as an Xcode user by default.Sokotra
I am strongly opposed to answering this question (let's not debate it please). No, you can't just "throw them into [your] project's sourcecode". You are going out of your way to make life as complicated and difficult for yourself as possible. I will not support that.Radiation
default clang compiler in xcode doesn't support c++17 yet, nor its experimental extensions.Nahshun
So can I obtain some subset of the experimental implementations and include them in my sourcecode?Sokotra
Yes, you can. That is what boost::any is.Radiation
Well for now I guess I have no choice. bcp gave me 777 header files to include when I asked for any.hpp. we hates it.Sokotra
I can understand why the desire not to include any part of Boost. But, well, lets not debate it. Not need to feel so strong about this issue. As to the question. C++17 support in Xcode 8.1 clang is unfortunately not very good. Some library features are available in experimental. For example <experimental/optional>. Apple stopped reporting which version of LLVM it is based on long ago. But if I have to guess, I would say LLVM release 3.7, bases on the _LIB_CPP macroPleader
I'm joking around a bit, I don't actually have "strong feelings". If I was going to use boost::any I would just do it (in fact I'm in the process of doing so now), but the point of the question is "can I do it without boost" without getting into "why not boost?" Don't worry I'm not crying or eating a pint of ice cream over it and I appreciate the help.Sokotra
@MatthewJamesBriggs I did not mean you feel too strong about. I meant no need to "strongly opposed to answering". If you really don't like the 777 header files, and any is only part you need from Boost, one way is to write your in-house version. Sometime it might be worth the effort. You can look into libstdc++, libc++, or Boost's implementation for inspirations. I used to maintain a subset of type_traits in my library for the same reason a few years ago. It's not that bad. However, only you and your team can decide the trade-offPleader
C
6

You can't say "I want the default Xcode compiler [which has no support for any]" and at the same time request it to support any. You also can't mix standard library headers for different compiler versions.

You can either

  • use a compiler version that provides std::any or
  • use any third party library that provides another any-like type.
Carline answered 11/12, 2016 at 20:51 Comment(0)
I
2

Your installation setup does not have the c++17 standard. std::any simply is not available to you unless you get a compiler with at least experimental support for what you want.

Clang Cxx Status

You'd have a lot better luck just using boost::any probably.

If you're really set on not bringing a third party library into play, the reality is that creating your own any isn't that difficult. I don't recommend reinventing the wheel but in this case it's not that difficult.

Here's a SO question with an answer showing a way to do 'any'.

Ichthyolite answered 11/12, 2016 at 21:3 Comment(0)
A
2

It is illegal to inject new types into std via a third party library. You can upgrade your compiler, get a distinct std library your compiler supports, or use a 3rd party library that provides any in another namespace, or write your own.

The first you said no to.

The second is hard, as xcode does not advertise what its compiler actually is. There are generally two common std libraries that work with clang-llvm derived compilers; libc++ and libstdc++. That kind of swap tends to be very expensive even if the other one has the feature you want.

The third is basically "use boost" or equivalent.

The last isn't hard; a few days work (mostly bugs after the fact), based on writing types of similar complexity, assuming "good enough" is good enough (ie, not getting caught up in ideal exception guarantees, or matching standard exactly, etc). An implementation will require hyperbolic effort to approach perfection, naturally.

Akel answered 11/12, 2016 at 21:16 Comment(0)
T
2

Xcode 9.0 beta can now be downloaded (https://developer.apple.com/download/). It supports the c++17 flag option.

Edit: Xcode 9.2 is publically available with std::any support.

Tidy answered 28/6, 2017 at 4:53 Comment(2)
@Zammbi any link you can provideHereabout
You have to login into Apple Developer: developer.apple.com/download Currently it is in beta 5. So shouldn't be too long before they release the final version.Tidy

© 2022 - 2024 — McMap. All rights reserved.