Use template to implement Conditional compilation

Source: Internet
Author: User
We know that Conditional compilation can be implemented using macros: # If 1
// Do something
# Else
// Do something else
# Endif

Using a template can achieve similar results: # include <stdio. h>

Template <bool value>
Void func ()
...{
If (value)
...{
Printf ("true ");
}
Else
...{
Printf ("false ");
}
}

Int main ()
...{
Func <true> ();
Return 1;
}

You may be worried that using if to determine whether it will affect performance. Let's look at the code generated by the compiler:
GCC compiler:
; G ++-O main. s main. cpp-S

. Globl _ z4funcilb1eevv
. Def _ z4funcilb1eevv;. SCL 2;. Type 32;. endef
_ Z4funcilb1eevv:
Pushl % EBP
Movl % ESP, % EBP
Subl $8, % ESP
Movl $ lc0, (% ESP)
Call _ printf
Leave
RET
. Def _ printf;. SCL 2;. Type 32;. endef
. Def _ z4funcilb1eevv;. SCL 3;. Type 32;. endef

It can be seen that there is no conditional judgment in the generated code after GCC compilation.

Try again using Microsoft's Compiler:
; Call E:/win2003_program/vs2005/VC/bin/vcvars32.bat
; Cl.exe/Fas main. cpp

; Function compile flags:/odtp
; Comdat ?? $ Func @ $00 @ yaxxz
_ Text Segment
?? $ Func @ $00 @ yaxxz proc; func <1>, comdat

; 5 :{

Push EBP
MoV EBP, ESP

; 6: If (value)

MoV eax, 1
Test eax, eax
Je short $ ln2 @ func

; 7 :{
; 8: printf ("True/N ");

Push offset $ sg3676
Call _ printf
Add ESP, 4

; 9 :}
; 10: else

JMP short $ Ln3 @ func
$ Ln2 @ FUNC:

; 11 :{
; 12: printf ("false/N ");

Push offset $ sg3678
Call _ printf
Add ESP, 4
$ Ln3 @ FUNC:

; 13 :}
; 14 :}

Pop EBP
RET 0
?? $ Func @ $00 @ yaxxz endp; func <1>
_ Text ends

Oh! The soft cl.exe still adds conditional judgment. Let's try again with the optimization options:
; Call E:/win2003_program/vs2005/VC/bin/vcvars32.bat
; Cl.exe/Fas main. CPP/O1

Public ?? _ C @ _ 05lfiobdml @ true? 6? $ Aa @; 'string'
Public ?? $ Func @ $00 @ yaxxz; func <1>
Extrn _ printf: Proc
; Comdat ?? _ C @ _ 05lfiobdml @ true? 6? $ Aa @
; File F:/practice/C_c ++/2008-01-03/testtemplate/Main. cpp
Const segment
?? _ C @ _ 05lfiobdml @ true? 6? $ Aa @ dB 'true', 0ah, 00 h; 'string'
; Function compile flags:/ogspy
Const ends
; Comdat ?? $ Func @ $00 @ yaxxz
_ Text Segment
?? $ Func @ $00 @ yaxxz proc; func <1>, comdat

; 6: If (value)
; 7 :{
; 8: printf ("True/N ");

Push offset ?? _ C @ _ 05lfiobdml @ true? 6? $ Aa @
Call _ printf
Pop ECx

; 9 :}
; 10: else
; 11 :{
; 12: printf ("false/N ");
; 13 :}
; 14 :}

RET 0
?? $ Func @ $00 @ yaxxz endp; func <1>
_ Text ends

Well, good! After the optimization option is added, no conditions are determined.

To sum up, using templates for Conditional compilation can be more flexible without any impact on efficiency.

Another problem is that you must add func <true> or func <false> to each call. You do not know how to set a default parameter. I tried the template <bool value = true> and the result cannot be compiled. If you have any good solutions, please let me know. Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.