boost::get() и содержимое variant
От: Аноним  
Дата: 13.08.14 07:51
Оценка:
Подскажите, есть ли способ безопасно проверить, действительно ли в контейнере variant содержится переменная того типа, который мы хотим получить из него с помощью get() ?
Вариант с wich() не нравится по вполне понятной причине: возвращаемое значение завязано на порядок перечисления типов в шаблоне при объявлении variant<>
Re: boost::get() и содержимое variant
От: jazzer Россия Skype: enerjazzer
Дата: 13.08.14 07:56
Оценка:
Здравствуйте, Аноним, Вы писали:

А>Подскажите, есть ли способ безопасно проверить, действительно ли в контейнере 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 (Skype: enerjazzer) Ночная тема для RSDN
Автор: jazzer
Дата: 26.11.09

You will always get what you always got
  If you always do  what you always did
Re[2]: boost::get() и содержимое variant
От: uzhas Ниоткуда  
Дата: 13.08.14 08:07
Оценка:
Здравствуйте, 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.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.