跟随包扩展有什么问题?
what's wrong with following pack expansion?
constexpr int ipow(int x, int n) {
return (n > 0) ? x * ipow(x, n - 1): 1;
}
template <char c>
constexpr int b3_helper() {
static_assert(c < '3', "not a ternary digit");
return c - '0';
}
template <char c, char... chars>
constexpr int b3_helper() {
static_assert(c < '3', "not a ternary digit");
return ipow(3, sizeof...(chars)) * (c - '0') + b3_helper<chars...>();
}
template <char... chars>
constexpr int operator"" _b3() {
return b3_helper<chars...>();
}
int main(){
int i = 201_b3;
return 0;
}
编译器说
在第12行呼唤'b3_helper'是模棱两可的";
我该如何解决它?当我学习C++编程语言 4th 时,我发现了这个问题。在第 560 页
歧义是因为像 b3_helper<'1'>
这样的调用有两个同样好的匹配项 - 第一个函数模板可以与 char c = '1'
匹配,第二个函数模板可以与 char c = '1'
匹配,...
是空参数包。要解决此问题,您可以将函数的"归纳"重载更改为需要两个或多个char
,如下所示:
template <char c, char d, char... chars>
constexpr int b3_helper() {
static_assert(c < '3', "not a ternary digit");
return ipow(3, 1 + sizeof...(chars)) * (c - '0') + b3_helper<d, chars...>();
}
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 格式说明符C++有什么问题
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 我的堆栈和库存清单程序的结构有什么问题?
- 此工厂功能有什么问题?
- 以下 C++ 代码有什么问题?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?
- 它解决了什么问题,对于非真空初始化,生命周期在初始化之前就开始了