Does the following code structure or design implementation have an Idiomatic Name?

女生的网名这么多〃 提交于 2021-02-10 05:33:30

问题


Within the C++ language, many will come across various design patterns that have a designated name in which we would call an Idiom, such as SFINAE, RAII, CRTP, Polymorphism, etc...

Consider this following code snippet in its most basic form...

template<typename T>
auto make_object = [](T val) {
    class Foo {
    public:
        Foo(T val) : value_{val} {}
        T value() const { return value_; }
    private:            
        T value_;
    };
    Foo foo(val);
    return foo;
};

// in some other code block, scope, or translation
int main() {
    auto bar = make_object<float>( 3.1f );
    auto val = bar.value();
    return 0;
}

Here I'm using a templated lambda to generate or create an instance of a class Foo that is fully contained(declared and defined) within the scope of the Lamba body's implementation and through the use of auto type deduction variable bar is of type Foo where you can treat it and access it as if you had declared Foo as its own separate class... Does this type of design pattern have a specific idiomatic name that I'm not aware of?

来源:https://stackoverflow.com/questions/66079425/does-the-following-code-structure-or-design-implementation-have-an-idiomatic-nam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!