关闭

gcc -E 预编译模板类

标签: gcc -E
410人阅读 评论(0) 收藏 举报
分类:
gcc -E 预编译模板类


需要的文件数:
simple-> ls
app.cpp  app.h  make.sh  temp.h  type.h
simple->


1) type.h
simple-> cat type.h
/// @file type.h
/// @brief 
/// @author EastonWoo
/// 0.01
/// @date 2015-11-19


#pragma once

#define MACRO_JOIN( X, Y ) MACRO_DO_JOIN( X, Y )
#define MACRO_DO_JOIN( X, Y ) MACRO_DO_JOIN2(X,Y)
#define MACRO_DO_JOIN2( X, Y ) X##Y

2) temp.h
simple-> cat temp.h
/// @file temp.h
/// @brief 
/// @author EastonWoo 
/// 0.01
/// @date 2015-11-19


// #pragma once   // 这个不能要, 因为这个文件相当于inline

#include "type.h"

#define SIGNAL_SIGNAL MACRO_JOIN(TSignal, SIGNAL_NUMBER)

#if (SIGNAL_NUMBER != 0)
template <SIGNAL_CLASS_TYPES>
#endif
class SIGNAL_SIGNAL
{
public:
    T3 add (T1 a, T2 b) {
        return (a + b);
    }
private:
    T1 m_t1;
    T2 m_t2;
    T3 m_t3;
};


#undef SIGNAL_SIGNAL

3) app.h
simple-> cat app.h
/// @file app.h
/// @brief 
/// @author EastonWoo 
/// 0.01
/// @date 2015-11-19


#pragma once

// TSignal1
#define SIGNAL_NUMBER 1
#define SIGNAL_CLASS_TYPES typename T1, typename T2, typename T3
#include "temp.h"
#undef  SIGNAL_NUMBER
#undef  SIGNAL_CLASS_TYPES

// TSignal2
#define SIGNAL_NUMBER 2
#define SIGNAL_CLASS_TYPES typename T1, typename T2, typename T3
#include "temp.h"
#undef  SIGNAL_NUMBER
#undef  SIGNAL_CLASS_TYPES

4) app.cpp
simple-> cat app.cpp
/// @file app.cpp
/// @brief 
/// @author EastonWoo 
/// 0.01
/// @date 2015-11-19

#include <stdio.h>
#include <string>

#include "app.h"

int main(int argc, const char *argv[])
{
    TSignal1<int, int, int>* signal1 = new TSignal1<int, int, int>;
    int iret = signal1->add(1, 2);
    printf("iret = %d\n", iret);

    TSignal2<std::string, std::string, std::string>* signal2 = new TSignal2<std::string, std::string, std::string>;
    std::string sret = signal2->add("hello ", "world");
    printf("sret = %s\n", sret.c_str());
    return 0;
}

5) make.sh
simple-> cat make.sh
#!/bin/bash - 
#===============================================================================
#
#          FILE: make.sh
# 
#         USAGE: ./make.sh 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: YOUR NAME (), 
#  ORGANIZATION: 
#       CREATED: 11/19/2015 20:29
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error

g++ -std=c++0x app.cpp -o app && ./app

6) 运行:
simple-> sh make.sh
iret = 3
sret = hello world
simple->

7) gcc -E app.h
simple-> gcc -E app.h 
# 1 "app.h"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "app.h"







app.h:8:9: warning: #pragma once in main file
 #pragma once
         ^
       




# 1 "temp.h" 1
# 10 "temp.h"
# 1 "type.h" 1







       
# 11 "temp.h" 2




template <typename T1, typename T2, typename T3>

class TSignal1
{
public:
    T3 add (T1 a, T2 b) {
        return (a + b);
    }
private:
    T1 m_t1;
    T2 m_t2;
    T3 m_t3;
};
# 14 "app.h" 2






# 1 "temp.h" 1
# 15 "temp.h"
template <typename T1, typename T2, typename T3>

class TSignal2
{
public:
    T3 add (T1 a, T2 b) {
        return (a + b);
    }
private:
    T1 m_t1;
    T2 m_t2;
    T3 m_t3;
};
# 21 "app.h" 2



0
0

猜你在找
【套餐】Hadoop生态系统零基础入门
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:191341次
    • 积分:2713
    • 等级:
    • 排名:第13252名
    • 原创:80篇
    • 转载:12篇
    • 译文:0篇
    • 评论:23条
    最新评论