[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] C Makro er på flere linier



On 20:19 Mon 13 Feb 2006, Rune Larsen wrote:
> Carsten Svaneborg wrote:
> 
> > Martin Møller Pedersen wrote:
> >> Hvorfor vil du bruge #if #endif ? Nedenstående virker da fint.
> > 
> > Det kommer an på hvad x er, og precedence af de operatorer
> > der kunne indgå i x.
> > 
> > 
> >> #define PRINT_DEBUG(x,y) \
> >> if x<DEBUG_LEVEL cout << "debug: \t " << y << endl; \
> > 
> > if (x)<DEBUG_LEVEL ..
> > 
> 
> det var i håb om helt undgå debug koden i programmet hvis ikke DEBUG var
> slået til. Det ovenstående vil vel altid give en instruktion med eller uden
> debug, medmindre kompileren er klog nok til at slette koden hvis 
> if (x < DEBUG) evaluerer til false. 

Som andre skriver vil compileren med stor sansynlighed fjerne noget
sådant, men hvis du bruger en gammel ikke-optimerende compiler, eller af
anden grund vil fjerne sådanne defines vil følgende kunne gøre som du efterlyser:


__
/* 0=ingen debug */
#define DEBUG 1

#if DEBUG!=0
#define PRINT_DEBUG(level, x) PRINT_DEBUG_ ## level(x)
#else
#define PRINT_DEBUG(level, x) /* ingenting */
#endif

#define PRINT_DEBUG_1(x) \
  std::cerr << "level 1 " << # x  << " : " << (x) << endl;

#define PRINT_DEBUG_2(x) \
  std::cerr <<  "level 2 " <<  # x  << " : " << (x) << endl;

/*  Du kan så kalde PRINT_DEBUG som herunder.
 */
PRINT_DEBUG(1, foo || bar && (hop && bof))
PRINT_DEBUG(2, a || b)
__

Prøv at køre ovenstående gennem gcc -E for at se resultatet.

Venligst
/Anders

-- 
"It's today!" said Piglet.
"My favorite day," said Pooh.



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2006-03-01, 02:01 CET [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *