Home > Software engineering >  Is there a Standard Untokenize Templated Type?
Is there a Standard Untokenize Templated Type?

Time:12-19

Is there a standard type to untokenize a type? It'd probably be implemented as so:

template<class T>
using untokenize = T;

This way I can perform the following cast using an overloaded operator:

struct x {
  int y;
  operator int&() {
    return y;
  }
};

x a;
// int&(a); // doesn't work
// (int&)(a); // not the same thing
untokenize<int&>(a); // works great

Or is there another, more standard way to achieve my goal of avoiding a c-style cast in favor of a function-style cast?

CodePudding user response:

static_cast<T> does what you're asking for.

CodePudding user response:

It would be more idiomatic to use the correct "named cast" in general. Here, static_cast is appropriate:

static_cast<int&>(a);

That said, there is a standard template that works the same as your other approach, std::type_identity, but only as of C 20:

std::type_identity_t<int&>(a);
  • Related