auto x = 10; //x is inferred to be int
auto y = "Hello"s; //y is inferred to be std::string
auto z = true; //z is inferred to be bool
//define object
auto person = {
auto const name = "Rafi"s;
auto age = 55;
};
//access members
std::cout << person.name << ", " //person.name is std::string
<< person.age << "\n"; //person.age is int
template<typename ... Ms>
void print(std::object<Ms...> const & obj) {
//access member-values as: obj.get<Ms>() ...
//access member-names as: obj.name<Ms>() ... OR Ms::name() ....
//access member-types as: typename Ms::type ...
((std::cout << Ms::name() << " = " << obj.get<Ms>() << " [" << nicefy(typeid(Ms::type)) << "]\n") , .... ); //using C++17 fold-expression
//which prints
//
// name = Rafi [std::string]
// age = 55 [int]
//Or stdlib itself could provide utility like:
std::iterate(obj, [](auto const & attr) { std::cout << attr.name() << " = " << attr.value() << " [" << nicefy(attr.type()) << "]\n"; });
}
auto person = {
const name = "Rafi"s,
&age = value,
const & expertise = "Playback Singing"s
};
#include <cson/cson.h++>
//define the labels first
CSON_LABEL(name);
CSON_LABEL(age);
auto x = cson(name = "Rafi", age = 55, work = "Singing"); //define object
C++11 introduced the keyword auto which demonstrates a very simple concept or idea... and that is, if you have a value, then you can create an object out of it whose type will be inferred by the compiler.
auto x = 10; //x is inferred to be int
auto y = "Hello"s; //y is inferred to be std::string
auto z = true; //z is inferred to be boolThat is simple, isn't it? But can we extend this simple idea to create objects of complex/composite types with many attributes/components? How about creating an object with 3 components, name and age, and let the compiler infer (and define) a suitable type to represent the object?
//define object
auto person = {
auto const name = "Rafi"s;
auto age = 55;
};
//access members
std::cout << person.name << ", " //person.name is std::string
<< person.age << "\n"; //person.age is int
From this, the compiler instantiates a class template std::object<> describing person in such a way that programmers could introspect its members, their types and names. Here is an example,
template<typename ... Ms>
void print(std::object<Ms...> const & obj) {
//access member-values as: obj.get<Ms>() ...
//access member-names as: obj.name<Ms>() ... OR Ms::name() ....
//access member-types as: typename Ms::type ...
((std::cout << Ms::name() << " = " << obj.get<Ms>() << " [" << nicefy(typeid(Ms::type)) << "]\n") , .... ); //using C++17 fold-expression
//which prints
//
// name = Rafi [std::string]
// age = 55 [int]
//Or stdlib itself could provide utility like:
std::iterate(obj, [](auto const & attr) { std::cout << attr.name() << " = " << attr.value() << " [" << nicefy(attr.type()) << "]\n"; });
}
struct {std::string const name = "Rafi";int age = 55;} person;Although `auto` is not possible in classes because that would complicate implementations by a lot apparently (I heard this somewhere, forgot where).
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/92a68b32-4be9-4fb9-b0f6-b071503ed698%40isocpp.org.
Why not allow this instead?auto person = struct { string name = "Bill"; short age = 25; };
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAJe%2Baq_cqTKEVAwjBJR_Nvaw%2Bj24uSjsWGzC5J%2BMYVUCfzCzbQ%40mail.gmail.com.
int x = 10; int y = 20; std::string u = "test"; auto l = [x, &y, z = u + " expression" ](){}
int x = 10;
int y = 20;
std::string u = "test";
class _some_compiler_gen_weired_name {
const int x; // param#1 'x' captured by copy
const int &y; // param#2 '&y' captured by ref
const std::string z; // param#3 'u + " expression"' captured by value
public:
_some_compiler_gen_weired_name (int _p1, int& _p2, std::string _p3 ) :
x(_p1), y(_p2), z(_p3) {}
};
auto l = _some_compiler_gen_weired_name (x, y, u + " expression");
auto person = [name = "Bill"s, age = 25]; // <- allow () and [] be optional in lambda definition.
int m = person.age;
auto l = [x, &y, z = u + " expression" ];
int k = l.x + l.y + l.z.size() + l.w;
auto person2 = [name = "Bill"s; age = short(25)]; // forcing the string and short types
auto person3 = const [name = "Bill"s; age = short(25)]; // making const membersconst auto person3 = [name = "Bill"s; age = short(25)]; // members are not const, the variable 'person3' is.
template<typename T>
void foo(const T&...p) {
cout << p.a << " " << p.b << " " << p.c;
}
foo( [a=1, b=2, c=3] ); // Yeah ! you can call a function too !
foo( [a="rhs structured bindings ?", b="or heterogeneous initialization list ?" , c="or named parameters?" ] );
auto lambda = [x, y] {
int z = 8;
int square(){ return z*z; };
};
lambda.x = lambda.square() + lambda.y;