Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@frederick-vs-ja
Copy link
Contributor

这里改进的写法看上去不需要修改 ss,可能更“干净”一点。

原理:

  • Itanium ABI 上变量 ss::a 的重整名为 _ZN2ss1aE
  • 在 MSVC 上需要用 #pragma 告诉链接器使用 MSVC ABI 的重整名 ?a@ss@@3HA

Godbolt link

还有另一个作弊的写法,不过大概不符合要求:

#include<iostream>

namespace ss {
    int a = 0;
}

int main() {
    enum ss { a = 100 };
    std::cout << ss::a << '\n';
}

原理:
Itanium ABI 上变量 ss::a; 的重整名为 _ZN2ss1aE 。
在 MSVC 上需要用 pragma 告诉链接器使用 MSVC ABI 的重整名 ?a@ss@@3Ha
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我又学习了

@Mq-b
Copy link
Owner

Mq-b commented Dec 16, 2023

#include<iostream>

namespace ss {
    int a = 0;
}

int main() {
    enum ss { a = 100 };
    std::cout << ss::a << '\n';
}

这种利用名字查找优先级的方式的确不太合适。

@Mq-b Mq-b merged commit f7c957e into Mq-b:main Dec 16, 2023
@frederick-vs-ja frederick-vs-ja deleted the hack-with-mangled-names branch December 16, 2023 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants