C++ constexpr在编译时不求值
我试图在编译时加密字符串,但该字符串似乎仍然存在于可执行文件的数据中 我禁用了所有优化,但它仍然存在C++ constexpr在编译时不求值,c++,C++,我试图在编译时加密字符串,但该字符串似乎仍然存在于可执行文件的数据中 我禁用了所有优化,但它仍然存在 class str { public: template<unsigned int Size> constexpr str(wchar_t(&String)[Size], wchar_t KeyCharacter) : m_Buffer{}, m_Key{}, m_Size{} { this->m_Size = Size;
class str {
public:
template<unsigned int Size>
constexpr str(wchar_t(&String)[Size], wchar_t KeyCharacter)
: m_Buffer{}, m_Key{}, m_Size{} {
this->m_Size = Size;
this->m_Key = KeyCharacter;
this->SetString(String);
}
~str() {
}
template<unsigned int Size>
constexpr void SetString(wchar_t(&String)[Size]) {
for(unsigned int i{}; i < this->m_Size; ++i) {
this->m_Buffer[i] = String[i] ^ this->m_Key;
String[i] = L'\0';
}
this->m_Buffer[this->m_Size] = L'\0';
}
constexpr wchar_t* GetString() {
return this->m_Buffer;
}
wchar_t m_Buffer[250];
wchar_t m_Key;
unsigned int m_Size;
};
int main() {
wchar_t unstr[] = { L"Hello, world!" };
str s(unstr, L'a');
getchar();
return 0;
}
类str{
公众:
模板
constexpr str(wchar\u t(&String)[Size],wchar\u t KeyCharacter)
:m_Buffer{},m_Key{},m_Size{}{
此->m_Size=尺寸;
此->m_Key=KeyCharacter;
此->设置字符串(字符串);
}
~str(){
}
模板
constexpr void SetString(wchar\u t(&String)[大小]){
for(无符号整数i{};im_Size;++i){
this->m_Buffer[i]=字符串[i]^this->m_键;
字符串[i]=L'\0';
}
此->m_缓冲区[此->m_大小]=L'\0';
}
constexpr wchar_t*GetString(){
返回此->m_缓冲区;
}
wchar_t m_缓冲区[250];
wchar_t m_键;
无符号整数m_大小;
};
int main(){
wchar_t unstr[]={L“你好,世界!”};
strs(unstr,L'a');
getchar();
返回0;
}
顺便说一句,代码不会计算为memcpy调用,这只是伪代码的解释
如您所见,hello world文本仍然存在,但在SetString函数中,我将其清空。它在运行时为空,但仍显示在二进制文件中
这是怎么回事?为什么绳子还在那儿?如何修复它?在编译时-您应该使用
constexpr str dds{unstr,L'a'}代码>您确定可以constexpr类实例吗?我的编译器不允许me@orORorOR:您需要删除无意义的析构函数。如果我将其设置为constepxr,则参数必须为const,然后我无法覆盖它。您说您已禁用所有优化,但实际上,如果启用了所有优化,编译器在编译时可能会进行更多计算。此外,“编译时求值”和“即使只将程序数据传递给constexpr
函数,也不要将程序数据输出到可执行文件”不是一回事。除非优化可执行文件的大小,否则编译器通常不会删除程序数据,因为在执行程序时无法观察到效果。