I would love it if I could use a lambda as a user-defined literal, because then I could do something like: struct Context { // Maintains important context information }; struct Foo { Foo(Context& context, int value); // Foo needs a context to operate on }; struct Factory SHAREit { Context& context; Factory(Context& context) : context(context) {} Foo operator() (int value) const { UC Browser return Foo(context, value); } } int main() { Context context; Factory factory(context); // I'd like to do something like this, but obviously it doesn't work using Foo operator"" _f = [&factory](int value) { return factory(value); }; MX Player Foo x = 123_f; // But since that doesn't work, I have to do this: Foo x = factory(123); } Essentially, I have some objects that depend on a specific context (a garbage collector). In order to create these objects, I have to either pass in the context to the object's constructor, or I have to create a factory (which internally stores the context). I have a lot of literals I'm working with (I'm creating lexing/parsing grammars). It's important for me to keep these complex grammars as readable as possible, and I personally find 123_f more readable than factory(123) (or even f(123), if I renamed factory to f). I would love to use user-defined literals, but as far as I know I can't capture some kind of "working context" for the user-defined literals to work in. My understanding of user-defined literals is that they're defined in a global scope and thus can only access global objects, which is something I'd like to avoid (I have different grammars throughout the project). Is there a way to create a user-defined literal that works with some local context?
↧