Creating a template inner lambda function?

Istvan Orban

I been trying to solve the next functional problem in C++:

template <typename T>
auto createFunction(T& ClosuredData) {
    template <typename U>
    return [&](function<void(T&, U&)> Callback) {
        return [&](U& DataFromCallback) {
            Callback(ClosuredData, DataFromCallback);
        };
    };
};

// example use
struct Person {
   string name{"Foo"};
   int age = 20;
}
Person foo{};
auto mutate = createFunction(foo);
auto changeAge = mutate<int>([](Person& person, int& age) {
    person.age = age;
})

changeAge(20)

So basically a template Function creator with an inner template lambda. But of course as far as I know inner templates are not possible in C++. I tried to make the callback as an auto type, but of course that way I can't infer the DataFromCallback resulting in false type-safety.

Can I somehow achieve a proper type safety without introducing a class/struct?

Edit: One of the "solution" I seen is using an auto type instead of a functional:

template <typename T>
auto CreateFunction(T& ClosuredData) {
    return [&](auto Callback) {
        return [&, Callback](auto DataFromCallback) {
            Callback(ClosuredData, DataFromCallback);
        };
    };
};

This is really cool, but gives a false sense of typesafety, for example:

{
    Person person{};
    auto personMutator = CreateFunction(person);
    auto changeAge = personMutator([](Person& person, int age) {
        person.age = age;
    });

    changeAge(""); // Would not throw an error in this line, but at an upper stack

    changeAge(any); // same stuff
}
user7860670

It seems that you could use generic lambdas available since C++14 (which basically work as inner templates):

template <typename T>
auto CreateFunction(T& ClosuredData) {
    return [&](auto Callback) {
        return [&, Callback](auto DataFromCallback) {
            Callback(ClosuredData, DataFromCallback);
        };
    };
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creating a lambda function dynamically

From Dev

Creating C "Template" function

From

Passing a lambda into a function template

From Dev

Passing lambda to template function

From Dev

creating template function with different parameters

From Dev

Error with lambda in template member function

From Dev

Using a template with lambda function pointer

From Dev

Pass lambda as template function parameter

From Dev

Convert template function to generic lambda

From Dev

Can a lambda instantiate a template function?

From Dev

Variadic template function accepting lambda

From Dev

Inner class of a template class friend function

From Dev

Calling template function from inner class

From Dev

Angular 5: creating a parent component with an external template and overriding the inner part

From Dev

How to pass template parameter of a function to a lambda in the function

From Dev

Creating a lambda function to iterate collections simultaneously

From Javascript

Creating a lambda function in AWS from zip file

From Dev

Creating lambda function to invoke glue job

From Dev

Creating a REST API from Lambda Function

From Dev

Creating a packaged task for a lambda function with iterators as arguments

From Dev

Haskell Lambda help - Creating a applications function

From Dev

Lambda function for creating an EC2 instance

From Dev

lambda function uses its parameter as template parameter calling template function

From Dev

Is it possible to pass variable template to function through lambda?

From Dev

Can a template function deduce a lambda's arguments?

From Dev

Is it possible to return a variadic lambda from a function template?

From Dev

Lambda functions with template parameters, not in function parameters

From Dev

Differences between function template and lambda expression

From Dev

What is the correct way to return a template lambda function?

Related Related

  1. 1

    Creating a lambda function dynamically

  2. 2

    Creating C "Template" function

  3. 3

    Passing a lambda into a function template

  4. 4

    Passing lambda to template function

  5. 5

    creating template function with different parameters

  6. 6

    Error with lambda in template member function

  7. 7

    Using a template with lambda function pointer

  8. 8

    Pass lambda as template function parameter

  9. 9

    Convert template function to generic lambda

  10. 10

    Can a lambda instantiate a template function?

  11. 11

    Variadic template function accepting lambda

  12. 12

    Inner class of a template class friend function

  13. 13

    Calling template function from inner class

  14. 14

    Angular 5: creating a parent component with an external template and overriding the inner part

  15. 15

    How to pass template parameter of a function to a lambda in the function

  16. 16

    Creating a lambda function to iterate collections simultaneously

  17. 17

    Creating a lambda function in AWS from zip file

  18. 18

    Creating lambda function to invoke glue job

  19. 19

    Creating a REST API from Lambda Function

  20. 20

    Creating a packaged task for a lambda function with iterators as arguments

  21. 21

    Haskell Lambda help - Creating a applications function

  22. 22

    Lambda function for creating an EC2 instance

  23. 23

    lambda function uses its parameter as template parameter calling template function

  24. 24

    Is it possible to pass variable template to function through lambda?

  25. 25

    Can a template function deduce a lambda's arguments?

  26. 26

    Is it possible to return a variadic lambda from a function template?

  27. 27

    Lambda functions with template parameters, not in function parameters

  28. 28

    Differences between function template and lambda expression

  29. 29

    What is the correct way to return a template lambda function?

HotTag

Archive