开发者

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

开发者 https://www.devze.com 2022-12-26 16:30 出处:网络
Can we write a portable include guard that doesn’t use the prepr开发者_如何学Pythonocessor in C++? If so how could that be done?No.

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

精彩评论

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