2

I'm trying to find out whether it's safe to use #import (instead of #include with include guards) in a cross-platform C++11 library?

The articles I found on SO and elsewhere seem to indicate that #import was proposed to be included in the C++11 standard, yet judging by answers from around ~2012 it still seemed to be Microsoft and GCC specific extensions with import not being an accepted standard.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217

1 Answers1

6

No, there's no "#import" in the current C++ standard.

The syntax has often been proposed as part of a module system for C++, but so far there hasn't been sufficient consensus on how such a system should work in detail.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • out of curiosity : do you have a link to such proposal? – BЈовић Sep 15 '13 at 13:07
  • 1
    @BЈовић: [Here is one](http://www.youtube.com/watch?v=4Xo9iH5VLQ0), and [in slides](http://isocpp.org/blog/2012/11/modules-update-on-work-in-progress-doug-gregor). – Kerrek SB Sep 15 '13 at 13:10
  • 2
    Maybe they should have instead proposed it as "a really simple solution to get rid of the ridiculous include guard idiocy". – CodeSmile Sep 15 '13 at 13:11
  • 1
    @LearnCocos2D `#pragma once`? – Angew is no longer proud of SO Sep 15 '13 at 13:12
  • 2
    @Angew Isn't that platform/compiler-specific, too? I just don't understand why the compiler would even consider including the same header twice. – CodeSmile Sep 15 '13 at 13:13
  • @LearnCocos2D: STL once said that "the C preprocessor actually does a pretty good job for the problem that it solves". I think given how hard it seems to be to work out a better solution, he has a point. – Kerrek SB Sep 15 '13 at 13:14
  • @LearnCocos2D Yes it is implementation-specific, but MSVC, gcc, icc and Clang support it. I don't know about others, but they probably do as well; [Wikipedia](http://en.wikipedia.org/wiki/Pragma_once) confirms this. – Angew is no longer proud of SO Sep 15 '13 at 13:18
  • From what I can tell, the word "import", or any inflection thereof, never appears in the standard. – Kerrek SB Sep 15 '13 at 13:26
  • The real problem with "solving" the #include problem (I'm far from convinced there is a real problem, that isn't going to continue existing with another system, unless we redefine the C and C++ languages dramatically) is that you basically have to retain all the functionality in the current system, but hiding it under some other guise. And in the testing that I've done (posted in an answer here), there is no HUGE performance problem caused by including a moderate number of headers. If you are including LOTS of headers in some way, then perhaps that is the problem, not #include in itself. – Mats Petersson Sep 15 '13 at 13:35