【问题标题】:Extract static member type from class of local variable从局部变量的类中提取静态成员类型
【发布时间】:2021-10-04 11:51:49
【问题描述】:

是否可以从局部变量的类中提取静态成员类型?也就是

class A {
 public:
  typedef int constituent_type;
  constituent_type a;
  A(constituent_type _a) :a(_a) {};
}


int main() {
  auto a = A(42);

  // ... lots ... of other code; where I have long since forgottten, what the type of a really was

  std::max<a::constituent_type>(a, a); //<<<
}


【问题讨论】:

  • std::max 将推断其参数的类型并使用operator&lt; 进行比较 - 所以我不清楚你想要实现什么?
  • 请发布一个您实际需要的情况示例。

标签: c++ static-members


【解决方案1】:

您可以在 C++ 11 或更高版本中使用 decltype:

decltype(a)::x

现场演示:

https://godbolt.org/z/cTq9zhKxe

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    相关资源
    最近更新 更多