variant icon indicating copy to clipboard operation
variant copied to clipboard

Remove `get_type_index` in favour of more standard `which()`

Open artemp opened this issue 9 years ago • 6 comments

get_type_index returns index in reversed order which is confusing. which method is complaint with boost::variant.

In order to remove get_type_index we need to address last remaining issue - invalid_type value.

Proposed solution:

invalid_type == sizeof(Types...) + 1
// relevant test that needs upgrading
REQUIRE(variant_type{mapbox::util::no_init()}.get_type_index() == mapbox::util::detail::invalid_value);

artemp avatar Jan 27 '16 15:01 artemp

I was thinking about the invalid index value when attempting to use a smaller type for storing the index (#19).

std/tuple/boost/... use indices 0, ..., N-1, and size_t(-1) as invalid index. I think of that as the invalid type preceding valid types in the list (its index is -1, although unsigned).

mapbox::variant internally uses indices N-1, ..., 0, and size_t(-1) as invalid. In the same line of thinking, the invalid type follows valid types. index_type(-1) is awkward in comparisons when index_type is an arbitrary unsigned type, so while implementing that, I modified the internal indexing to N, ..., 1, and 0 as invalid index. It's simple and reliable with whatever type is used for storing the index.

lightmare avatar Jan 27 '16 16:01 lightmare

@lightmare - thanks. I can see it will work but I'm a bit hesitant to just go and change internal index to N,...,1 Lets get a consensus on this :) @joto ?

artemp avatar Jan 28 '16 14:01 artemp

@artemp how is this related to #67? Can one be closed in favor of the other?

springmeyer avatar Jan 28 '16 18:01 springmeyer

Currently there is still the public get_type_index() function, so this internal index is visible outside. I suggest we mark this function as deprecated and remove it in 2.0 and then revisit this issue. @artemp?

joto avatar Jan 29 '16 14:01 joto

ok with me

/cc @joto @lightmare @springmeyer

artemp avatar Jan 29 '16 14:01 artemp

/agree, shouldn't be changed until it's truly internal.

lightmare avatar Jan 29 '16 14:01 lightmare