标签:c language-lawyer variadic-templates template-meta-programming user-defined-lite
这是我将二进制文字转换为十进制的实现:
template<char Head, char... Tail>
constexpr int operator"" _b()
{
if constexpr (sizeof... (Tail) == 0)
{
return Head - '0';
}
else
{
return (Head - '0') * (1 << sizeof...(Tail)) + operator"" _b<Tail...>();
}
}
prog.cc:1:2: error: template parameter list for literal operator must be either 'char...' or 'typename T, T...'
template<char Head, char... Tail>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:19:27: error: no matching literal operator for call to 'operator""_b' with argument of type 'unsigned long long' or 'const char *', and no matching literal operator template
std::cout << 110110110_b;
^
error: a literal operator template must have a template parameter list equivalent to "<char ...>"
constexpr int operator"" _b()
^
<source>(2): error C3686: 'operator ""_b': literal operator template must have exactly one template parameter that is a parameter pack
因此,icc需要char …而clang和msvc需要typename T,T …或char …,只有gcc允许我的头和尾.
解决方法应该很简单—-只需替换char Head,char …使用char …数字尾部并返回一个新的aux函数,使用char Head,char … Tail作为模板参数,或者使用struct然后专攻头部和头部,尾巴……如果没有constexpr.
但我没有从标准草案中找到相关要求.你能告诉我哪一个符合标准吗?当然,如果你有更优雅的解决方案(除了上面提到的两个),它不会调用编译器错误,请粘贴在这里,我会非常appreicate.
解决方法:
标准在[over.literal]/5中明确说明:
The declaration of a literal operator template shall have an empty parameter-declaration-clause and its template-parameter-list shall have a single template-parameter that is a non-type template parameter pack with element type
char
.
因此GCC允许这样做是错误的.
标签:c,language-lawyer,variadic-templates,template-meta-programming,user-defined-lite 来源: https://codeday.me/bug/20190727/1549012.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。