char 和初始化列表

2024-04-22

I'd like to pass some numeric byte values via an initializer list a variadic template into an array. Is that possible?

template < int N > struct a {
  char s[N];
  template < typename ... A >
  a (A ... _a) : s {_a...} {}
};

int main () {
  // g++-4.5: error: narrowing conversion of »_a#0« from »int« to »char« inside { }
  a < 3 > x { 1, 2, 3 };
}

我能想到的是

  • 使用八进制表示,'\001'等,或者
  • 投射每一个值。

但两者都不能令人满意。


您不需要任何复杂的代码

template < int N > struct a {
  char s[N];
  template < typename ... A >
  a (A ... _a) : s {static_cast<char>(_a)...} {}
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

char 和初始化列表 的相关文章

随机推荐