Is there a way to refactor C++11 code into C++ code that can be compiled by a less capable compiler?
Asked Answered
T

1

12

This has implications on code portability of C++11 code in general. I am talking about having a new code generation stage akin to the C/C++ preprocessor stage wherein we can make the generated C++ code more "friendly" to a larger family of compilers. I thought perhaps this can potentially have a effect on how feasible it is to move people over to coding in C++11.

Game consoles are an example of a type of platform where you may be stuck on a limiting compiler that perhaps may not support nice C++11 features. Another example could be Intel's C++ compiler. I'm very glad to see that Intel has been working hard on this and it looks like this is a non-issue on the latest version, but suppose for some reason one needed to use an older version of the compiler!

Another semi-concrete example, is one of my recent projects has been to build a diff tool for front-line use in development; I became dissatisfied with the traditional line-based diffs so I built a little command-line tool that uses a diff_match_patch implementation in C++. C++11 has "permeated" into the glue code because auto is just too cool, but the consequence was that I basically had to go and build LLVM, Clang, and libc++ on Linux in order to start using my tool on linux. It's not really clear how to even distribute C++11 projects to Linux in source form because it is a bit of an ordeal to set clang up on Linux. This is not to say that it can't be done, but it's just too bleeding edge, and one has to spend a few hours to get that toolchain set up there. (Update: Well, a few months/years later, that same little tool now builds perfectly fine with GCC provided by centos 7's packages, so that's a relief.)

So, if I had a magical way to auto-refactor the C++11 tool to generate old-school C++ code that an older compiler would accept out of the box, this can potentially make life a little easier for some people maybe.

There's been some amazing progress on automating code refactoring with clang, and this is well-presented in David's answer here, and so it has got me thinking that perhaps there's the possibility of setting up a specific self-contained tool based off of this technology that can effectively transform C++11 code patterns into corresponding non-C++11 equivalents. The idea here being that instead of using that for general purpose automated refactoring (e.g. isolating and refactoring code where strings are converted to C-strings and back to strings), we might be able to use that for isolating the use of unsupported C++11 features and refactoring them into some code that does the same thing but would actually successfully compile.

Is this something that is possible? I'm not too familiar with the capabilities of C++11 and what the consequences would be in terms of down-grading to "old" C++ that implements the same functionality. It certainly seems like if this sort of thing was possible, I couldn't do it, because I don't have (neither do I expect I will ever have) enough of a grasp on the language to know how to implement this.

I suppose a more appropriate question would be, is this worth the effort? It is hard to see how (given that LLVM clearly is very capable at cross-compiling to ARM) one might be prevented from the use of clang to get the job done using more direct methods such as cross-compiling to the target paltform. My guess is that this is simply my paranoia speaking and I won't really need to worry in the future about getting my C++11 code running smoothly across all conceivable target platforms as they should all be either x86, x86-64, or ARM anyway. But who knows what my job will be in a few years.

Tappet answered 4/7, 2013 at 22:25 Comment(10)
possible duplicate of Is there a C++11 to C++03 converter?Blink
Ah damn, indeed the question has already been asked.Tappet
Instead of converting code, look into something like boost.config that will handle the compiler differences.Haslet
Basically you are asking to trade your pain of working with C++11 for somebody else's pain of building a C++11 to legacy C++ translator. As somebody who is interested in tools and specifically in translators, and just so happens to have C+11 and legacy C front ends and general translator machinery, I'm probably the guy you are thinking about. I understand your motivation. What's mine?Prescind
@JesseGood Could you expand a bit on how boost.config might help for this?Tappet
@IraBaxter I can't see one. Call my question short-sighted if you want, that it is in a certain sense. I can't think of a problem in static-linking libc++.Tappet
@StevenLu: It's all in the docs. They have a ton of macros that can detect whether a compiler supports or doesn't support a C++11 feature and other macros that expand to different things depending on whether a feature is supported or not. You can then use these to write code for a C++11 and C++03 compiler. Doing this will be much easier than attempting to convert code.Haslet
Ah, okay so basically going through and making the code maximally portable the normal hard way. Fair enough. At that point I'd just be writing C++03 (as a C++11 compiler can tackle that just fine)Tappet
@StevenLu: What's static-linking libc++ got to do with whether one can build such a language translator tool, or its worth? Your first two sentences made perfect sense and then you veered into outerspace, so I can't tell what you were actually thinking.Prescind
Yes you are totally right, it makes very little sense to be concerned about that. I'm not sure i could adequately explain why that concerned me. I think what I thought was that on the hypothetical platform that had no viable C++11 compiler, perhaps obtaining a working libc++ static lib might be challenging. But now I realize that that really could be no more challenging than any other cross compiling that would succeed on such a platform. a libc++ there that works is useless without an app that uses it. So this is a non-issue.Tappet
D
2

Also, g++ has supported increasingly large parts of C++11 for years. 4.8.1 has pretty complete support, according to http://gcc.gnu.org/projects/cxx0x.html and most of the most interesting parts have been available much longer (e.g. auto and variadic templates are in 4.4, custom string literals in 4.5, and constexprs in 4.6).

It may be easier to install (or build, if necessary) a newer version of gcc than trying to refactor your code.

Dime answered 13/7, 2013 at 1:10 Comment(1)
A very serviceable option as well.Tappet

© 2022 - 2024 — McMap. All rights reserved.