Why is std :: get <T> for `variant` a global function?

Can someone tell me why std::get<T> for C ++ 17 is a global function and not a member function member variant<...> ?

+5
source share
1 answer

If get<T>() was a member function template woudl keyword is needed when it is called in a dependent context. For instance:

 template <typename Variant> void f(Variant const& v) { auto x0 = v.template get<T>(); // if it were a member auto x1 = get<T>(v); // using a non-member function } 

Even without a using declaration or get() directives, if found as std::variant<...> and get() , they are declared in the std . Thus, there seems to be no good reason to make it a member function, since a global function is easier to use.

+6
source

Source: https://habr.com/ru/post/1263853/


All Articles