您的位置:首页 > 其它

gcc -E 预编译模板类

2015-11-19 21:17 281 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: