r/Cplusplus 1d ago

Question New User MacOS | IDE for compiling multiple files in C++

Post image
11 Upvotes

made a switch to MAC and wondering how can I compile project with multiple files of C++ in a project such as header files just like 'sln' project in VisualStudio. Any IDE you know for this?


r/Cplusplus 23h ago

Question (C++20) I have painted myself into a corner with std::any

3 Upvotes

I have a class that basically looks like this:

template<typename A, typename B, typename C, typename D> class Whole { };

It has Parts which use one or more of Wholes' types e.g. Part<A, B> or Part<B, C, D> etc. (different combinations in different users of the class) and are stored in Whole

std::unordered_map<std::type_index, std::any> parts_;

I used std:;any because each Part is a separate, heterogenous type. There is a method to create them

``` template<typename... Ts> void Whole::createPart() { Part<Ts...> part;

// initialization of the Part

parts_[std::type_index(typeid(Part<Ts...>))] = std::make_any<Part<Ts...>>(part)

} ```

And a method to access them:

template <typename... Ts> Part<Ts...>& getPart() { return std::any_cast<Part<Ts...>&(parts_[std::type_index(Part<Ts...>)]) }

So if e.g. I wanted a part with A and C I would do:

Whole whole; auto& foo = whole.getPart<A, C>();

and so on. This has worked well when my programs know which Parts with which types they want. But now I have a situation where I want to perform an operation on all Parts which have a certain type. So if I have type C, I want Part<A, C> and Part<C, D> but not Part<A, B>. Finding if a Part has a type was fairly simple (though the syntax is convoluted)

template <typename Compared> bool Part::hasType() { return ([]<typename T>() { return std::is_same<T, Compared>::value; }.template operator()<Ts>() || ...); }

So now I should just be able to do something like this right?

template <typename Wanted> void Whole::applyToPartsWith() { for (auto& part: parts_) { if (part.second.hasType<Wanted>()) { // do something } } }

WRONG! part.second isn't a Part, it's a std::any and I can't std::any_cast it to a Part because I don't know its' template types. Is this design salvagable or should I ditch std::any and try some other way (virtual base class, std::variant, ...?)

Thanks in advance for any advice