开发者

Can we write a portable include guard that doesn’t use the preprocessor in C++?

Can we write a portable include guard that doesn’t use the prepr开发者_如何学Pythonocessor in C++? If so how could that be done?


No.

  1. You cannot use #include without the preprocessor.
  2. Without preprocessor directives, including the same file twice will always result in the same sequence of tokens.

There are a couple non-portable ways to do this (both use the preprocessor), such as:

#pragma once

and

#import "file.h"

But header guards work everywhere, and your compiler is probably optimized to check for header guards so it won't even bother processing a duplicate #include directive.


That's a bit of a non-starter. If you're #including files you are stuck using the preprocessor, regardless. The closest thing you to what you ask that I'm aware of is the #pragma once preprocessor directive, but that's not strictly portable - although it is widely available - and it of course relies on the preprocessor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜