如何从折叠表达式中获取索引
How to get index from fold expression
我正在制作一个简单的constexpr字符串编码器,见下文。
template<char...Chars>
struct encoder
{
constexpr static char encode(char c)
{
return c ^ size;
}
constexpr static size_t size = sizeof...(Chars);
constexpr static const char value[size + 1] = {encode(Chars)...,0};
};
template<typename T,T...Chars>
constexpr auto operator""_encode()
{
return encoder<Chars...>::value;
}
用途:
"啊"_encode
"123"_encode
我想从编码函数中获取字符索引,像这样
constexpr static char encode(char c,uint32_t index)
{
return c ^ (size + index);
}
或者像这样
template<uint32_t index>
constexpr static char encode(char c)
{
return c ^ (size + index);
}
但我不知道怎么做。有人告诉我怎么做吗?
您可以在 C++17 中的单个constexpr
函数中编写整个内容:
template<typename T, T...Chars>
constexpr auto operator""_encode()
{
constexpr std::size_t size = sizeof...(Chars);
std::array<char, size+1> ret = {}; // Maybe T instead of char?
int i = 0;
((ret[i] = Chars ^ (size + i), i++), ...);
ret[size] = 0;
return ret;
}
(为了每个人的理智,我让它返回一个std::array
而不是一个内置数组。
这是一个 godbolt 链接,包括您的一个测试输入(如果您包含所需的输出会有所帮助,没有人喜欢仔细研究 ASCII 表并手动xor
东西,即使我在这里这样做(:
https://godbolt.org/z/P8ABHM
另外,请不要使用它来加密任何东西。
相关文章:
- C++17:如何在并行 STL 中获取工作项的索引
- Direct3D 11 - HLSL - 获取顶点索引 ID
- 在 std::variant 中按类型获取索引
- CPtrList - 如何获取元素的索引?
- 特征获取索引数组,其中向量中的值为真(不需要循环)
- 如何从折叠表达式中获取索引
- C++如何获取索引I处的元素,并检查它在向量或数组中是否为空
- 无法获取索引缓冲区以在 OpenGL 中绘制正方形
- 从索引缓冲区中获取索引
- 如何从QTreeView中的项目获取索引
- 从 C++ 数组元素的指针获取索引的最快方法是什么
- 算术在3D矢量中获取索引
- 获取索引信息的问题-SQL Server
- 从迭代器获取索引处的元素
- 在C++中,执行去引用和获取索引0的操作是相同的
- Q_PROPERTY WRITE获取索引
- 2d查找重复元素/获取索引位置并计数重复项
- 获取索引时qmodelindex的父参数
- Rgd:用于获取索引位置的矢量迭代器
- 获取索引和元素-向量问题