获取成员类型
注意:这个问题最初是在 2012 年提出的。在 decltype
说明符被任何主要编译器完全实现之前。除非您只能访问 C++03,否则您不应该查看此代码。所有主要的 C++11 兼容编译器现在都支持 decltype。
有没有一种简单的方法来检索成员的类型?
在 C++03 中,
struct Person
{
std::string name;
int age;
double salary;
};
int main()
{
std::vector<Person> people; // get a vector of people.
std::vector<GET_TYPE_OF(Person::age)> ages;
ages.push_back(people[0].age);
ages.push_back(people[10].age);
ages.push_back(people[13].age);
}
我实际上是这样做的(即稍微懒惰):
#define BuildType(className, member, type) \
struct className ## member: TypeBase<className, type> \
{ \
className ## member() \
: TypeBase<className, type>(#member, &className::member) \
{} \
}
BuildType(Person, name, std::string);
BuildType(Person, age, int);
BuildType(Person, salary, double);
typedef boost::mpl::vector<Personname, Personage, Personsalary> FunckyMTPMap;
但我不想强制用户指定成员的类型,而是希望编译器实用地生成它。
#define BuildType(className, member) \
struct className ## member: TypeBase<className, TYPE_OF(className ## member)> \
{ \
className ## member() \
: TypeBase<className, TYPE_OF(className ## member)>(#member, &className::member)\
{} \
}
BuildType(Person, name);
BuildType(Person, age);
BuildType(Person, salary);
typedef boost::mpl::vector<Personname, Personage, Personsalary> FunckyMTPMap;
NOTE: This question was originally asked way back in 2012. Before the decltype
specifier was fully implemented by any major compilers. You should not be looking at this code unless you only have access to C++03. All major C++11 compliant compilers now support decltype
.
Is there an easy way to retrieve the type of a member?
In C++03
struct Person
{
std::string name;
int age;
double salary;
};
int main()
{
std::vector<Person> people; // get a vector of people.
std::vector<GET_TYPE_OF(Person::age)> ages;
ages.push_back(people[0].age);
ages.push_back(people[10].age);
ages.push_back(people[13].age);
}
I am actually doing this (ie being slightly lazy):
#define BuildType(className, member, type) \
struct className ## member: TypeBase<className, type> \
{ \
className ## member() \
: TypeBase<className, type>(#member, &className::member) \
{} \
}
BuildType(Person, name, std::string);
BuildType(Person, age, int);
BuildType(Person, salary, double);
typedef boost::mpl::vector<Personname, Personage, Personsalary> FunckyMTPMap;
But rather than have to force the user to specify the type of the member I want to the compiler to generate it pragmatically.
#define BuildType(className, member) \
struct className ## member: TypeBase<className, TYPE_OF(className ## member)> \
{ \
className ## member() \
: TypeBase<className, TYPE_OF(className ## member)>(#member, &className::member)\
{} \
}
BuildType(Person, name);
BuildType(Person, age);
BuildType(Person, salary);
typedef boost::mpl::vector<Personname, Personage, Personsalary> FunckyMTPMap;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是C++11方式。它要求您使用
&Person::age
而不是Person::age
,尽管您可以轻松调整宏以使 & 符号隐式化。Is the C++11 way. It requires you to use
&Person::age
instead ofPerson::age
, although you could easily adjust the macro to make the ampersand implicit.在 C++2003 中,它不能直接完成,但您可以委托给一个推导类型的函数模板:
In C++2003 it can't be done directly but you can delegate to a function template which deduces the type:
因为在你的例子中你使用了 boost,所以我会使用 boost 中的 TYPEOF 。
http://www.boost.org/doc/libs/1_35_0 /doc/html/typeof.html
它的工作方式与 C++11 的 decltype 非常相似。
http://en.wikipedia.org/wiki/C%2B%2B11#Type_inference
在您的情况下:
您可以将 decltype 或 BOOST_TYPEOF 为您提供的类型与 typeinfo 进行比较,
您需要制作一个长度大于 14 的适当人员向量才能使示例正常工作。
gcc 有 typeof 或 typeof 做同样的事情。
作为旁注。对于您给出的示例,如果上述内容都不与您相关,您可以只在结构中定义类型。
然后使用
std::向量<人::年龄类型>年龄;
Since in your examples you use boost I'd use TYPEOF from boost.
http://www.boost.org/doc/libs/1_35_0/doc/html/typeof.html
it works very similarly to decltype of C++11.
http://en.wikipedia.org/wiki/C%2B%2B11#Type_inference
in your case:
you can compare the types decltype or BOOST_TYPEOF gives you with typeinfo
you need to make a proper people vector with length >14 for the example to work.
gcc has typeof or typeof doing the same thing.
As a side note. For the example you gave you could just define the types in the struct instead if none of the above is relevant for you.
then use
std::vector< Person::agetype > ages;