Home > Software engineering >  How to alias std::get
How to alias std::get

Time:01-18

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 ?

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related