Why does std::get_if(std::variant) take pointer as an argument instead of reference?

512 views
Skip to first unread message

Victor Dyachenko

unread,
Jul 28, 2016, 4:54:53 AM7/28/16
to ISO C++ Standard - Discussion
From the current draft:

template <class T, class... Types>
std
::add_pointer_t<T> get_if(variant<Types...> *pv);

template <class T, class... Types>
std
::add_pointer_t<const T> get_if(const variant<Types...> *pv);

 Why the arguments are pointers? It is not efficient. The implementation has every time to check if the argument is not nullptr. Does nullptr argument makes any sense at all?

This functions can be class members or take a reference as an argument. What are reasons for suboptimal design like this? Just mimic dynamic_castinterface?

Victor Dyachenko

unread,
Oct 6, 2016, 3:54:53 AM10/6/16
to ISO C++ Standard - Discussion

From: Axel Naumann <Axel.Naumann (at) cern.ch>
Sent: Thursday, October 06, 2016 10:35 AM
To: Victor Dyachenko <victor.dyachenko (at) gmail.com>
Subject: Re: Fwd: Why does std::get_if(std::variant) take pointer as an argument instead of reference?

 

Hi Victor,

I think the reason was pointer-in, pointer-out symmetry, which you could indeed describe as "mimic dynamic_cast". I find that a strong reason.

The reduction in efficiency from the nullptr check is minimal given that the function needs a runtime check for the index anyway. We could have made it a pre-condition but then get_if couldn't be noexcept anymore.

The most performant and recommended interface is visitation.

Cheers, Axel 

 

 

 

On 10/06/2016 09:25 AM, Victor Dyachenko wrote:

--
ROOT - http://root.cern
EP-SFT, CERN, 1211 Geneve 23, Switzerland
Tel: +41 22 7678225
Reply all
Reply to author
Forward
0 new messages