C++ 使用constexpr混淆std::array
我正在寻找一个小函数,它能够通过增加值来转换C++ 使用constexpr混淆std::array,c++,variadic-templates,template-meta-programming,constexpr,stdarray,C++,Variadic Templates,Template Meta Programming,Constexpr,Stdarray,我正在寻找一个小函数,它能够通过增加值来转换std::array。函数必须是编译时函数 我能够为长度为3的数组编写一个小的constexpr函数,但无法将其推广到任意长度的std::arrays。我也未能将它概括为包含与chars不同的内容 有人知道怎么做吗 #include <array> #include <iostream> #include <valarray> constexpr std::array<char,3> obfuscate
std::array
。函数必须是编译时函数
我能够为长度为3的数组编写一个小的constexpr函数,但无法将其推广到任意长度的std::array
s。我也未能将它概括为包含与char
s不同的内容
有人知道怎么做吗
#include <array>
#include <iostream>
#include <valarray>
constexpr std::array<char,3> obfuscate(const std::array<char,3>& x) {
return std::array<char, 3>{x.at(0)+1, x.at(1) + 2, x.at(2) + 3 };
}
/* Won't compile
template<typename T,typename S, template<typename, typename> L=std::array<T, U>>
constexpr L<T,U> obfuscate(const L<T, U>& x) {
return {x.at(0) + 1, x.at(0) + 2, x.at(0) + 3 };
}
*/
std::ostream& operator<<(std::ostream& str, const std::array<char, 3>& x) {
for (auto i = 0; i < 3; i++) {
str << x.at(i);
}
return str;
}
int main(int argc, char** args) {
std::array<char, 3> x{ 'a','b','c' };
std::cout << x << std::endl;
std::cout << obfuscate(x) << std::endl;
// std::cout << obfuscate<3>(x) << std::endl;
}
#包括
#包括
#包括
constexpr std::array混淆(const std::array&x){
返回std::数组{x.at(0)+1,x.at(1)+2,x.at(2)+3};
}
/*不会编译
模板
constexpr L混淆(const L&x){
返回{x.at(0)+1,x.at(0)+2,x.at(0)+3};
}
*/
std::ostream&operator有一些方法使用元组包,这些方法非常好,除了MSVC在编译大型字符串时存在性能问题
我发现这种折衷在MSVC中效果很好
template<typename I>
struct encrypted_string;
template<size_t... I>
struct encrypted_string<std::index_sequence<I...>>
{
std::array<char, sizeof...(I)+1> buf;
constexpr static char encrypt(char c) { return c ^ 0x41; }
constexpr static char decrypt(char c) { return encrypt(c); }
constexpr explicit __forceinline encrypted_string(const char* str)
: buf{ encrypt(str[I])... } { }
inline const char* decrypt()
{
for (size_t i = 0; i < sizeof...(I); ++i)
{
buf[i] = decrypt(buf[i]);
}
buf[sizeof...(I)] = 0;
return buf.data();
}
};
#define enc(str) encrypted_string<std::make_index_sequence<sizeof(str)>>(str)
模板
结构加密字符串;
模板
结构加密字符串
{
std::阵列buf;
constexpr静态字符加密(字符c){返回c^0x41;}
constexpr静态字符解密(char c){返回加密(c);}
constexpr explicit\uuu forceinline加密字符串(const char*str)
:buf{encrypt(str[I])…}{}
内联常量char*decrypt()
{
对于(大小i=0;i
以后的某个地方
auto stringo = enc(R"(
kernel void prg_PassThru_src(const global unsigned short * restrict A, int srcstepA, int srcoffsetA,
global float * restrict Beta, int srcstepBeta, int srcoffsetBeta,
int rows, int cols) {
int x = get_global_id(0);
int y0 = get_global_id(1);
if (x < cols) {
int srcA_index = mad24(y0, srcstepA / 2, x + srcoffsetA / 2);
int srcBeta_index = mad24(y0, srcstepBeta / 4, x + srcoffsetBeta / 4);
Beta[srcBeta_index] = A[srcA_index];
}
}
//somewhere later
cv::ocl::ProgramSource programSource(stringo.decrypt());
auto-stringo=enc(R)(
内核void prg_PassThru_src(const global unsigned short*restrict A,int srcstepA,int srcoffsetA,
全局浮点*限制Beta、int srcsteppeta、int srcfoffsetbeta、,
整数行,整数列){
int x=获取全局id(0);
int y0=获取全局id(1);
if(x
你可以看到这家伙关于更复杂方法的谈话:
您可以使用std::index\u序列
:
template<class T, std::size_t N, std::size_t... Is>
constexpr std::array<T, N> helper (const std::array<T, N> &x, std::index_sequence<Is...>) {
return std::array<T, N>{static_cast<T>(x.at(Is)+Is+1)...};
}
template<class T, std::size_t N>
constexpr std::array<T, N> obfuscate(const std::array<T, N> &x) {
return helper(x, std::make_index_sequence<N>{});
}
模板
constexpr std::array helper(const std::array&x,std::index_序列){
返回std::数组{static_cast(x.at(Is)+Is+1)…};
}
模板
constexpr std::array混淆(const std::array&x){
返回帮助程序(x,std::make_index_sequence{});
}
你是想对数组进行模糊处理还是让程序正常运行?我有一大堆关于前者的代码,尽管它通常会杀死大型软件包的MSVC编译器。@Mikhail:我真的很想对字符串进行模糊处理,这样就没人能读取编译文件中的字符串了。我的字符串通常很短。我只是想知道是可以做到的。@Holt:我必须坚持使用C++11。但你的解决方案是什么?它是编译时表达式吗?目的是什么?模糊处理不是加密,攻击者不难解开你的字符串,所以为什么还要麻烦呢?我很好奇在编译时能做什么。很明显,这是可以破解的足够努力的模糊器。干得好。似乎是正确的方法。不幸的是,它似乎没有模糊字符串?知道为什么吗?我调整了我的operator@Aleph0您正在使用x.at(0)
在您自己的代码中为每一项添加代码。我只是复制了它。这是打字错误吗?很抱歉,这是一种类型。我想在数组中添加递增的数字。刚刚更正了我的错误。效果很好。我真的需要一些时间来消化您的解决方案。看来,我可以从中学到很多东西。我想知道是否有可能以模糊(“字符串”)的方式使用它但是我需要一个方法来将常量字符*转换成std::array
?