【问题标题】:What are the requirements on type for this template function这个模板函数对类型有什么要求
【发布时间】:2011-04-10 06:47:37
【问题描述】:

我正在查看如下所示的 C++ 代码:

template<class A>
bool foo(int A::*)
{ /*blah*/ }

int A::* 构造是什么?它对A 类型有什么要求?

非常感谢!!

【问题讨论】:

标签: c++ generics function


【解决方案1】:

int A::* 是指向int 类型为A 的数据成员的指针。例如,给定类型:

struct Foo { int i; };
struct Bar { double d; };
  • int Foo::* 是指向 int 类型为 Foo 的数据成员的指针,其唯一有效值是 null 和 Foo::i 的地址
  • int Bar::* 是指向 int 类型为 Bar 的数据成员的指针,其唯一有效值为 null,因为 Bar 不包含 int 数据成员

A 类型的唯一要求是它不是原始类型,因为原始类型显然不能有数据成员。

【讨论】:

    猜你喜欢
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    相关资源
    最近更新 更多