问题
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