小编val*_*ien的帖子

struct成员的模板函数

有没有办法编写能够在给定的不同成员上运行的单个模板函数struct

一个错误的例子如下:

struct Foo
{
  int a, b;
}

template <MEMBER x> //which does not exist 
cout_member(Foo foo)
{
  cout << foo.x << endl;
}

int main()
{
  Foo foo; 
  cout_member<a>(foo);
  cout_member<b>(foo);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想象一个基于交换机的答案,但我想知道这个交换机是否会在运行时(我想避免)或编译时进行测试?

c++ templates struct switch-statement

6
推荐指数
1
解决办法
403
查看次数

标签 统计

c++ ×1

struct ×1

switch-statement ×1

templates ×1