0

Is it possible to "alias" std::get in something like as it would give something like bool v = as<bool>(my_variant);

from C++11: How to alias a function? I understand well that we can not use using because a function template is not a type.

from C++ How to create a function pointer to an overloaded templated function I understand that a function pointer can only be created from a specific template instantiation..

So I tend to say that this is not possible, but maybe I'm wrong ?

orhtej2
  • 2,133
  • 3
  • 16
  • 26
sandwood
  • 2,038
  • 20
  • 38
  • 2
    Are you looking for `template using as=std::get`? – Sam Varshavchik Jan 17 '23 at 16:57
  • 2
    @SamVarshavchik That should only work for types, I think. – HolyBlackCat Jan 17 '23 at 16:57
  • 10
    You'll have to implement a function template with the same signature that forwards its arguments to `std::get`. But what is the point except to confuse anyone who is aware of `std::get`? – user17732522 Jan 17 '23 at 16:58
  • as explained in other tickets, a function template forwarding to the original function is only a (poor) "equivalent" of what an alias is. – sandwood Jan 17 '23 at 17:06
  • @sandwood What problem does it have? (I didn't immediately see what comment/answer in the linked question you refer to.) There are no proper aliases for functions or function templates in C++. And function pointer/references to standard library functions are also a bad idea, because it is unspecified whether they will work for any given specialization. (The standard library is allowed to change the overload set or signatures from those given in the specification as long as direct calls behave the same way.) – user17732522 Jan 17 '23 at 17:11
  • *a function template forwarding to the original function is only a (poor) "equivalent" of what an alias is.* - so is an alias basically. But it is alright if you are trying to customize some generic code that needs to "cast" or "convert" some generic variable to a particular type (in your case). What is the use you want an alias for? – Sergey Kolesnik Jan 17 '23 at 17:11

1 Answers1

5

For functions and function templates, in C++, there are no proper aliases in the sense of the alias name immediately referring to the same entity as it aliases.

The closest thing is a function reference, which also only applies to individual specializations and still behaves like a reference, not an alias. Also, taking a pointer or reference to a standard library function has unspecified behavior in order to allow the implementation to modify the overload set. Only direct calls are generally specified.

Practically speaking implementing as as a new (set of) function templates that forward to std::get in their body is the best approach. The behavior of as can be made to be identical to that of std::get by copying all of std::get's specified signatures.

Also, it is generally impossible to alias templates. Type alias templates (template</*...*/> using MyAlias = /*...*/;) do not alias a class template with a new name. They are instead themselves separate templates and each specialization of that template is a single alias for a specific type.

user17732522
  • 53,019
  • 2
  • 56
  • 105
  • Can't one simply `template auto get2(Ts&&...Vs) {return std::get(std::forward(Vs)...);}` ? – Mooing Duck Jan 17 '23 at 18:25
  • 1
    @MooingDuck No, `std::get` expects the first template argument to be given explicitly. And there are overloads with type and non-type explicit template parameters. So at least two overloads are required. I don't think copying really all overloads is required though. And by "identical behavior" I meant to include SFINAE-friendliness, which requires writing out the signatures or adding constraints. – user17732522 Jan 17 '23 at 18:28
  • Thanks for the clarification. Also I was not aware that it was UB to take a reference to a std function. – sandwood Jan 18 '23 at 07:40
  • @sandwood Not _undefined behavior_ (UB), just _unspecified behavior_. In particular it is unspecified which overload the reference or pointer will choose or whether it can be chosen at all (it could be ambiguous). You have a similar issue with taking references/pointers to any library's function by the way. If the library adds an overload, taking the reference may become ambiguous or unintentionally select one you don't expect because overload resolution is applied differently than in direct calls. – user17732522 Jan 18 '23 at 19:25