zoukankan      html  css  js  c++  java
  • C/C++预编译阶段获得sizeof(xx)的值

    譬如目标平台是arm的板子,arm-none-eabi-g++作为编译器。手头没有板子来运行代码,而代码中又需要确定sizeof(xx)的值。如下代码包含了两种方法。

    #include <iostream>
    #include <stdio.h>
    #include <cmath>
    #include <stdint.h>
    
    //方法1:用warning搞
    //https://stackoverflow.com/questions/20979565/how-can-i-print-the-result-of-sizeof-at-compile-time-in-c
    char (*__kaboom)[sizeof( int* )] = 1;  //改成你需要的类型
    char (*__kaboom)[sizeof( int32_t* )] = 1;
    
    
    //方法2,用C++的模板参数搞。来自xfan
    #include <cstddef>
    template <std::size_t x>
    struct show_size;
    
    void foo()
    {
        show_size<sizeof(char)>(); //改成你需要的类型
    }
    
    using namespace std;
    
    int main() {
    
            cout << "hello " << endl;
    
            return 0;
    }
    
  • 相关阅读:
    KVM快速构建虚拟机
    工程师测试
    配置SMB,NFS
    Shell脚本基础应用
    Web网页部署
    基础邮件,mariadb数据库
    SElinux,firewalld配置
    Linux管理员测试
    磁盘分区
    配置权限,LDAP
  • 原文地址:https://www.cnblogs.com/zjutzz/p/13398197.html
Copyright © 2011-2022 走看看