如何解决如何阅读复杂的类型?
我知道使用类型别名和 typedef 使代码可读性强、不易出错且易于修改。但是在这个例子中我想知道这个复杂的类型:
#include <iostream>
#include <typeinfo>
char ( * (* x() )[] )();
int main(){
std::cout << typeid(x).name() << std::endl;
}
正如经验法则所说,我是从内到外阅读它,但我觉得它太混乱了。
有人可以帮我读一下吗?
GCC 的输出:
FPA_PFcvEvE
解决方法
char - returning a char *
(* - of pointers to *---v ^
(* - returning a pointer to *---v ^ v ^
x() - a function `x` --^ v ^ v ^
)[] - an array *---^ v ^
)(); - functions *--^
并参见 https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.array-type ,例如:
FPA_PFcvEvE
^ - function (
^ - returning pointer
^ - to an array [
>< - with no dimension expression
^ - separator,end of array ]
^ - of pointers
^ - to functions (
^ - returning char
^ - (void) functions that take no arguments
^ - separator,end of function )
^ - (void) it's a function that takes on arguments
^ - separator,end of function )
示例:
#include <iostream>
char return_a() { return 'a'; }
char return_b() { return 'b'; }
char return_c() { return 'c'; }
char (* array_of_funcs_that_return_char[3])() = {
return_a,return_b,return_c,};
char (* (*pointer_to_array_of_funcs_that_return_char)[3])() = &array_of_funcs_that_return_char;
char ( * (* x() )[3] )() {
return pointer_to_array_of_funcs_that_return_char;
}
int main() {
std::cout << (*x())[1](); // outputs 'b'
}
,
我制作了一个小型库来帮助将复杂类型解码为英语-y std::string 值。只需调用 type_to_string::describe<T>()
,它就会返回一个(主要是)人类可读的字符串,描述类型 T。
这是你的类型:
// url-includes works with compiler explorer,not normal c++
#include <https://raw.githubusercontent.com/cuzdav/type_to_string/main/describe.hpp>
#include <iostream>
int main() {
using X = char ( * (*() )[] )();
std::cout << type_to_string::describe<X>() << std::endl;
}
它的输出是:
function taking (),returning pointer to array[] of pointer to function taking (),returning char
现场观看 https://godbolt.org/z/6Kjf5jb7E
(实现比作为 SO 答案的一部分有用的更多细节,但它可以在我的 github 上的链接上阅读。)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。