bob*_*eff 4 c++ templates user-defined-literals variadic-templates c++11
在《C++ 编程语言》书第 4版的第 19 章中,有一个使用模板技术定义三进制数字文字的示例,但该示例无法编译。我尝试以我认为正确的方式修复它,但它仍然无法编译。
#include <cstdint>
#include <iostream>
using namespace std;
constexpr uint64_t ipow(uint64_t x, uint64_t n)
{
return n > 0 ? x * ipow(x, n - 1) : 1;
}
template <char c>
constexpr uint64_t base3()
{
static_assert(c >= '0' && c <= '2', "Not a ternary digit.");
return c - '0';
}
template <char c, char... tail>
constexpr uint64_t base3()
{
static_assert(c >= '0' && c <= '2', "Not a ternary digit.");
return ipow(3, sizeof...(tail)) * (c - '0') + base3<tail...>();
}
template <char... chars>
constexpr uint64_t operator""_b3()
{
return base3<chars...>();
}
int main()
{
cout << 1000_b3 << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Clang给出以下错误:
error: call to 'base3' is ambiguous
return ipow(3, sizeof...(tail)) * (c - '0') + base3<tail...>();
^~~~~~~~~~~~~~
<source>:22:49: note: in instantiation of function template specialization 'base3<'0', '0'>' requested here
<source>:22:49: note: in instantiation of function template specialization 'base3<'0', '0', '0'>' requested here
<source>:28:10: note: in instantiation of function template specialization 'base3<'1', '0', '0', '0'>' requested here
return base3<chars...>();
^
<source>:33:15: note: in instantiation of function template specialization 'operator""_b3<'1', '0', '0', '0'>' requested here
cout << 1000_b3 << endl;
^
<source>:12:20: note: candidate function [with c = '0']
constexpr uint64_t base3()
^
<source>:19:20: note: candidate function [with c = '0', tail = <>]
constexpr uint64_t base3()
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
定义它的正确方法是什么?
目前,当tail只有 1 个字符时(当使用'0'用户定义的文字的最后一位数字调用时),它可以调用以下任一重载base3
template <char c>
constexpr uint64_t base3() // With c as '0'
template <char c, char... tail>
constexpr uint64_t base3() // With c as '0' and tail as an empty parameter pack
Run Code Online (Sandbox Code Playgroud)
没有理由偏爱其中之一,因此它是模棱两可的。
您需要第二个重载不能仅使用 1 个参数,因此您可以确保它至少需要 2 个参数:
template <char c, char second, char... tail>
constexpr uint64_t base3()
{
static_assert(c >= '0' && c <= '2', "Not a ternary digit.");
return ipow(3, 1+sizeof...(tail)) * (c - '0') + base3<second, tail...>();
}
Run Code Online (Sandbox Code Playgroud)
或者用 SFINAE 来做:
template <char c, char... tail>
constexpr
typename std::enable_if<sizeof...(tail) != 0, uint64_t>::type
base3()
{
static_assert(c >= '0' && c <= '2', "Not a ternary digit.");
return ipow(3, sizeof...(tail)) * (c - '0') + base3<tail...>();
}
Run Code Online (Sandbox Code Playgroud)
或者将其更改为 0 个字符的基本情况:
template <typename = void> // Can be called with an empty set of template args
constexpr uint64_t base3()
{
return 0;
}
template <char c, char... tail>
constexpr uint64_t base3()
{
static_assert(c >= '0' && c <= '2', "Not a ternary digit.");
return ipow(3, sizeof...(tail)) * (c - '0') + base3<tail...>();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77 次 |
| 最近记录: |