2

C++11 and C++14 introduce a lot of new features that make programmers' lives easier. However, in various environments (e.g. CUDA), support for the most modern C++ features may be weak or nonexistent.

Many of these features (e.g. auto, decltype, constexpr, variadic templates) do not add any challenges to code generation, and really only require a parser that understands the constructs. In principle, unless honestly new functionality (e.g. thread_local) is used, there could be a tool that takes C++14 code and converts it into something that could be built with something that only understands the C++03 language.

Does such a tool exist? (or are my presumptions way off base?)

  • Not that I'd know of...a very good idea though. – ArunasR Oct 25 '14 at 16:38
  • [_clang-modernize_](http://clang.llvm.org/extra/clang-modernize.html) may be ? – P0W Oct 25 '14 at 16:38
  • 2
    No, `clang-modernize` is doing the opposite conversion (old C++ to modern C++) – Basile Starynkevitch Oct 25 '14 at 16:40
  • clang/llvm has a C backend though, perhaps you could generate llvm bitcode and transform that into plain C. – Alexander Oh Oct 25 '14 at 16:41
  • 1
    I'm not sure how features such as `constexpr` could be mimicked in C++03 without any "challenges to code generation". It might be easier to answer this if you list the C++11 features you are interested in back-porting. – juanchopanza Oct 25 '14 at 16:41
  • @BasileStarynkevitch Sorry, didn't read the entire post, thanks – P0W Oct 25 '14 at 16:45
  • I'm at a loss; my problem is "I want to use C++14 but have to use C++03 or older", which sounds like something that is on topic. I can't imagine that the answer to my question can be anything other than "Too bad", "Write your own translator" or "You can do that with XXX". –  Oct 25 '14 at 17:09
  • If this is secretly "off topic because you should have posted on [another stackexchange site]"(https://softwarerecs.stackexchange.com/questions/13455/translating-new-c-to-old-c), then the secret should be revealed in the comments. I had no idea such a thing existed previously. And I would still not have known had I had anything else to do at the time rather than scroll through all of the non-accepted answers. –  Oct 25 '14 at 17:21
  • There's really no need to get so defensive. It's off-topic for a very clear reason: you're asking for a tool. The closure banner explains this. – Lightness Races in Orbit Oct 25 '14 at 20:19
  • Why are you writing C++14 code in the first place if you can't use it? Don't tell me you're going to try to backport features from something like Boost that rely on C++14? – Lightness Races in Orbit Oct 25 '14 at 20:20
  • @Lightness: Because I am writing the sorts of things that, if I was writing C++11, I could do so more easily, more readably, more conveniently, and be less error-prone, and it would be silly to write it in C++03 if I didn't have to. –  Oct 25 '14 at 20:28
  • @Hurkyl: Using `auto` and `constexpr` then relying on some utility to convert your code back into C++03 seems remarkably more error-prone and less convenient than simply writing valid C++03 in the first place. Just one of many problems that immediately come to mind is the difficulty you're going to cause yourself in debugging any of your programs when the source simply doesn't match up to what you're executing. – Lightness Races in Orbit Oct 25 '14 at 20:30
  • @Lightness: It would depend greatly on how good the tool is. If there were good tools for this, it could be relatively painless. And it would be painful if the tools were bad; I've had to program in environments where using C++ at all was inconvenient and error prone as compared to vanilla C due to poor compiler/linker support both for the language and its idioms. I won't know how painless or painful trying such a thing would be unless I actually had an opportunity to try it out. –  Oct 25 '14 at 20:45
  • It doesn't really matter how "painless" it feels: you're introducing a _huge_ new potential point of failure, and I simply cannot see how the miniscule gains could be anywhere near worthwhile. Granted, wanting to actually give it a try to find out for sure is admirable. I'm just saying I don't think I'd ever even consider this. C++03 isn't _that_ bad to program with and the improvements in C++11 and C++14 can almost completely be simulated or supplanted by third-party libraries. – Lightness Races in Orbit Oct 25 '14 at 20:46

1 Answers1

1

AFAIK no such tool exist. Some C++ compilers (the original Cfront, and perhaps Comeau C++) generated C code.

You might customize GCC (e.g. using MELT) to translate the internal Gimple representation to a small subset of C++. This is a lot of work (and the emitted C++ won't be portable).

Maybe you should consider OpenACC

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547