Подскажите, есть ли способ безопасно проверить, действительно ли в контейнере variant содержится переменная того типа, который мы хотим получить из него с помощью get() ?
Вариант с wich() не нравится по вполне понятной причине: возвращаемое значение завязано на порядок перечисления типов в шаблоне при объявлении variant<>
Здравствуйте, Аноним, Вы писали:
А>Подскажите, есть ли способ безопасно проверить, действительно ли в контейнере variant содержится переменная того типа, который мы хотим получить из него с помощью get() ?
RTFM:
Returns:
If passed a pointer, get returns a pointer to the value content if it is of the specified type U; otherwise, a null pointer is returned.
If passed a reference, get returns a reference to the value content if it is of the specified type U; otherwise, an exception is thrown (see below).
http://www.boost.org/doc/libs/1_56_0/doc/html/boost/get_idp308982624.html
Здравствуйте, jazzer, Вы писали:
J>RTFM:
добавлю отсюда
http://www.boost.org/doc/libs/1_56_0/doc/html/boost/variant.html#idp304024064-bb
variant queries
int which() const;
Returns:
The zero-based index into the set of bounded types of the contained type of *this. (For instance, if called on a variant<int, std::string> object containing a std::string, which() would return 1.)
Throws:
Will not throw.
bool empty() const;
Returns:
false: variant always contains exactly one of its bounded types. (See the section called “"Never-Empty" Guarantee” for more information.)
Rationale:
Facilitates generic compatibility with boost::any.
Throws:
Will not throw.
const std::type_info & type() const;
Returns:
typeid(x), where x is the the content of *this.
Throws:
Will not throw.
Notes:
Not available when BOOST_NO_TYPEID is defined.