获取成员类型

发布于 2024-12-27 04:58:30 字数 2062 浏览 1 评论 0原文

注意:这个问题最初是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨离汐 2025-01-03 04:58:30
template <class T, class M> M get_member_type(M T:: *);

#define GET_TYPE_OF(mem) decltype(get_member_type(mem))

是C++11方式。它要求您使用 &Person::age 而不是 Person::age,尽管您可以轻松调整宏以使 & 符号隐式化。

template <class T, class M> M get_member_type(M T:: *);

#define GET_TYPE_OF(mem) decltype(get_member_type(mem))

Is the C++11 way. It requires you to use &Person::age instead of Person::age, although you could easily adjust the macro to make the ampersand implicit.

江挽川 2025-01-03 04:58:30

在 C++2003 中,它不能直接完成,但您可以委托给一个推导类型的函数模板:

template <typename T, typename S>
void deduce_member_type(T S::* member) {
     ...
}

int main() {
    deduce_member_type(&Person::age);
}

In C++2003 it can't be done directly but you can delegate to a function template which deduces the type:

template <typename T, typename S>
void deduce_member_type(T S::* member) {
     ...
}

int main() {
    deduce_member_type(&Person::age);
}
多像笑话 2025-01-03 04:58:30

因为在你的例子中你使用了 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
在您的情况下:

std::vector<BOOST_TYPEOF(Person::age) > ages;

您可以将 decltype 或 BOOST_TYPEOF 为您提供的类型与 typeinfo 进行比较,

#include <typeinfo>
cout << typeid(obj).name() << endl;

您需要制作一个长度大于 14 的适当人员向量才能使示例正常工作。

gcc 有 typeof 或 typeof 做同样的事情。

作为旁注。对于您给出的示例,如果上述内容都不与您相关,您可以只在结构中定义类型。

struct Person
{
  typedef  int agetype;
  std::string name;
  agetype         age;
  int         salary;
};

然后使用
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:

std::vector<BOOST_TYPEOF(Person::age) > ages;

you can compare the types decltype or BOOST_TYPEOF gives you with typeinfo

#include <typeinfo>
cout << typeid(obj).name() << endl;

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.

struct Person
{
  typedef  int agetype;
  std::string name;
  agetype         age;
  int         salary;
};

then use
std::vector< Person::agetype > ages;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文