ETLCPP / etl

Embedded Template Library
https://www.etlcpp.com
MIT License
2.24k stars 392 forks source link

Define `ETL_USING_LEGACY_VARIANT` as a convenience macro for code that tries to support both new and legacy variants #920

Closed tigran2008 closed 3 months ago

tigran2008 commented 4 months ago

Right now one has to repeat the condition in <etl/variant.h>, e.g. #if !ETL_USING_CPP11 || defined(ETL_USE_LEGACY_VARIANT) but it would be nicer to do #if ETL_USING_LEGACY_VARIANT. Obviously, it would be as simple as #define ETL_USING_LEGACY_VARIANT !ETL_USING_CPP11 || defined(ETL_USE_LEGACY_VARIANT)

tigran2008 commented 4 months ago

(regarding supporting both new and old variants, it would be nicer if the new API had some stuff from the older one, e.g. is_type for the sake of reducing conditional compilation)

jwellbelove commented 4 months ago

Are there any other functions from the legacy API that you think may be relevant?

tigran2008 commented 4 months ago

Well, I personally haven't needed another test method myself yet, but if it wouldn't be difficult, I guess having is_supported_type, is_same_type and is_valid might be useful for others. (What does is_valid do, though?)

jwellbelove commented 4 months ago

It's impossible to have an is_valid that works the same as the legacy as, like std::variant, it default constructs the first type. The only way would be for the variant's first type to be etl::monostate, and detect this.

In the legacy variant, a default constructed object is 'invalid'.

jwellbelove commented 3 months ago

Fixed 20.39.3