auto_cast: Automatic type casting in C++

Published December 7, 2025

I: Abstract

This posts talks about an idea of mine called auto_cast.
Which consists of a structure casting a source type T into an automatic type Y.

The container itself is easy to implement in modern C++. Bear in mind that operator auto is set to be deprecated in future C++ releases.
As per https://cplusplus.github.io/CWG/issues/1878.html says.

II: Replacing operator auto: The case for templates.

Using the operator, you can cast away decltype(ref) to ret's type (which is long*).
That's the magic behind this class, no need to specify a target type.
You'd have to do the following:


int main() {
    auto ref = new long long(1337);
    long* ret = auto_cast<decltype(ref)>{ref};

    /// ...
}

The source code of auto_cast is available at: https://godbolt.org/z/dWTac9b35

III: Conclusion:

The auto_cast is an interesting idea about automatic casting if I'd give it a name, the idea about whether it's a good idea or not is up to debate though. And I am open to such debates.

Amlal.

References

  • https://cplusplus.github.io/CWG/issues/1878.html
  • https://godbolt.org/z/dWTac9b35
  • https://cplusplus.github.io/CWG/issues/1670.html