Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C++17 'any' with Xcode 8.1

Tags:

c++

c++17

stdany

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++?

like image 480
Matthew James Briggs Avatar asked Dec 11 '16 20:12

Matthew James Briggs


4 Answers

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

like image 42
Austin Jenkins Avatar answered Nov 18 '22 18:11

Austin Jenkins


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.
like image 159
Pixelchemist Avatar answered Nov 18 '22 19:11

Pixelchemist


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.

like image 2
Yakk - Adam Nevraumont Avatar answered Nov 18 '22 18:11

Yakk - Adam Nevraumont


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.

like image 2
Daniel Ryan Avatar answered Nov 18 '22 20:11

Daniel Ryan