7

Standard compliant C++ compilers define a __cplusplus macro which may be inspected during preprocessing to determine under what standard a file is being compiled, e.g:

#if __cplusplus < 201103L
#error "You need a C++11 compliant compiler."
#endif

#include <iostream>
#include <vector>

int main(){
    std::vector<int> v {1, 2, 3};
    for (auto i : v){
        std::cout << i << " ";
    }
    std::cout << std::endl;
    return 0;
}

My question is:

  • Is there a standard way to indicate what standard a source file should be compiled with?

That would allow build tools to inspect sources prior to compilation to determine the appropriate argument for -std= (cf. shebang's which can indicate scripting language/version: #!/usr/bin/env python3).

A non standard and brittle way I can think of is looking for the preprocessor checks of __cplusplus but in the example above I could also have written:

#if __cplusplus <= 199711L
#error "You need a C++11 compliant compiler."
#endif

hence, writing e.g. a regex would become quite tricky to catch all variations.

EDIT:

While I sympathize with the answer by @Gary which suggests relying on a build system, it assumes that we actually will have a build step.

But you can already today:

  • use an interpreter to run a C++ program using e.g. CINT
  • or use a source to source translation using e.g. rosecompiler

My question is also about indicating that the source is C++ and what version it was intended for (imagine someone digging out my code 70 years from now when C++ might be as popular as say Cobol is today).

I guess the equivalent thing I would be looking for is the C++ equiavlent of HTML's: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

6
  • Out of interest, why would you care? If you write code that is compilable in 98 it should be compilable in 03 or 11 (you could be using deprecated functions - potentially), but it should compile - so what is the purpose?
    – Nim
    Commented Oct 14, 2014 at 10:08
  • @Nim I guess it's about the minimum version the file requests.
    – leemes
    Commented Oct 14, 2014 at 10:09
  • @leemes, let me ask the question differently, if a file says it can be compiled with 98, does std= get changed to that for this file? If not, then this approach makes no sense IMHO, at the project level decide which version you are using, and possibly have a check in the file for handling older compilers (to reject if it's too old...)
    – Nim
    Commented Oct 14, 2014 at 10:39
  • @Nim I have a build script which I want to auto detect if my source is using C++11 so I don't have to care to set the appropriate -std flag. Usually you can just enforce the latest standard of your compiler, e.g. -std=c++11 but strictly speaking the C++11 standard has some backward incompatible changes. And in general I think it's good as documentation (e.g. C++ headers often end with '.h' so there may be some confusion with C headers unless you inspect them manually) Commented Oct 14, 2014 at 10:57
  • 2
    There's a proposal to standarize the requesting for features, take a look here. Commented Oct 14, 2014 at 11:44

1 Answer 1

3

C++ Standards in a way are somewhat like developing against a library. In that sense, libraries typically evolve in a way that slowly deprecates old functions while making access to new functions. The typical way is the introduction of new methods or signatures while still allowing access to the old ones.

As a simple example, for instance, you might make an app for the iPhone that is backwards compatible with IOS 4 and above. You don't get the option to cherry pick what specific versions you want to support. This is good because otherwise you open code evolution up to a matrix of possibilities, making your code harder to understand and maintain.

Alternatively, you may introduce preprocessor instructions to build certain pieces conditionally depending on a version or flag of some sort. These are temporary measures, however, and should be removed as the code evolves.

So I think for answering this question as is, the better question is asking oneself in this situation is what will adding something like this actually solve and will it add needless complexity (one of the code smells of bad design)?

In this situation and from experience, I personally think you're better sticking with one standard. I think you'll find that trying to differentiate standards by sprinkling various preprocessor #ifdef and #ifndefs is going to make understanding your code base difficult to understand and manage. Even if you had one include file with the definition of what version is allowed that gets included by all other files, it becomes yet another file to manage....not to mention when you change it you have to recompile everything that includes it.

If you're worried about someone building your code base with the wrong standard, use a build system that doesn't require developers to input that information. For instance Make, Ant, cmake. It makes the building of your software simple and clearly defines how the project should be compiled in a repeatable fashion. If you go this route, you'll see that trying to protect the code from being compiled improperly becomes a non-issue.

Also, if they go out of their way and compile with the wrong standard, they'll be greeted with plenty of compiler errors =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.