TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24782 14
Automatic casting with applications to extension methods and UFCS (language evolution)

INTRODUCTION

I'm a fan of UFCS, I probably think too much about it. Maybe here's a different direction towards UFCS.
The idea is that a simple language addition could (through some development)
lead to Extension Methods and UFCS.

This language addition might be called automatic casting.

CHAPTER 1: AUTO-CAST

Say that I want to call the std::string member function find_first_of on a const char*
string, "Hello3".

Of course, I can't call

"Hello3".findfirstof("123")

but it's easy if I first convert the C string into a std::string:

string("Hello3").findfirstof("123")

Maybe we could invent some syntax to tell the compiler to
auto-cast a const char* to a string, where needed:

// auto-cast const char to string.
auto operator string(const char
s){return string(s);}

We have entered the realm of MMINAE (missing member is not an error). If the
compiler can't resolve a member function, it will then apply the user-defined
auto-casts (in order) until the casted value can resolve the member function, which
is then used.

More complicated auto-casts can be defined. For example, this will
auto-cast an int to a string, by converting the digits to ASCII:

auto operator string(int n){return tostring(n);}

Then this allows code like:

(654321).find
firstof("123")

which will, after some MMINAE scanning, convert this code to:

to
string(654321).findfirstof("123")

CHAPTER 2: EXTENSION METHODS

I'd like to extend std::string by adding another member function, hasInt(int n).
Not by actually going into the <string> header file and adding a member function,
but by creating some code to give that illusion.

First, I define a helper class that provides the hasInt member function:

struct StringHasInt : public string {
bool hasInt(int n){
return this->contains(tostring(n));
}
};

Then define an auto-cast from a `string` to a `StringHasInt`:

auto operator StringHasInt(string s){return static
cast<StringHasInt>(s);}

Thus, when I call hasInt on a string:

string text;
...
text.hasInt(123);

MMINAE scanning will activate, and resolve the missing member by converting to the code:

staticcast<StringHasInt>(text).hasInt(123);

**CHAPTER 3: UFCS**

So, if we want to "do the UFCS" and would like to get from:

bool hasInt(const string s, int n){...etc...

to

text.hasInt(123)

by a simple macro call:

MAKE
UFCS(hasInt);

How is this done? The macro magic to convert this to a
helper class followed by an auto-cast is
left as an exercise to the reader!

https://redd.it/1r4z40v
@r_cpp
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
More from @r_cpp
  1. Oct 7, 2026Escaping `CreateProcess()` arguments on Windows https://holyblackcat.github.io/blog/2026/0…
  2. Oct 7, 2026flat_map internals https://quantdev.blog/posts/flat-map-internals/index.html https://redd.…
  3. Oct 7, 2026New C++ Conference Videos Released This Month - September 2026 C++Now 2026-08-31 - 2026-09…
  4. Oct 7, 2026Jean Frantz René: C++ Memory safety for safety-critical and non-safety critical applicatio…
  5. Oct 7, 2026Full Meeting C++ 2026 program released https://meetingcpp.com/mcpp/schedule/#2026 https://…
  6. Oct 7, 2026Qt moc now can handle basic C++ modules (Qt 6.12) https://codereview.qt-project.org/c/qt/q…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →