I have some C++11 code using the auto
inferred type that I have to convert to C++98. How would I go about converting the code, substituting in the actual type for all instances of auto
?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
As
auto
is known at compile-time, you need to interoperate with the compiler.One option would be the Clang compiler's LibTooling library that provides infrastructure that you can base static analysis tools on. For example, look at their refactoring example code that removes superfluous
.c_str()
calls from the code. I think you could write a similar tool that convertsauto
into the inferred type.You could use typeid and std::type_info::name();
The names aren't beautiful but you can at least translate them. These names are got from g++. The name is compiler dependent. There is some movement to standardize a pretty_name(). Here is a non-standard way to unmangle the names.
You can try to use the
BOOST_AUTO
macro in the Boost typeof library.becomes
An alternative approach would be to use function templates and type deduction. It may not work in all examples you have but it may help in some cases:
Change this to:
auto
is specified in terms of type deduction, so the above should have very similar, if not identical semantics as the C++ '11 version.It is going to be a PITA, but you can declare an incomplete struct template accepting a single type parameter.
Given the variable
x
you want to know the type of, you can use the struct withdecltype(x)
and that will lead to a compiler error that will show you the inferred type.For example:
Live demo
which will produce an error message in the form:
with
X
being the inferred type. In this particular case the type isint
.Trivia: This has been recommended by Scott Meyer in one of the recent NDC 2014's videos (I don't remember which one).