Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically rewrite c++11 auto keyword into derived types

Tags:

c++

c++11

auto

Maybe a strange question, but is there any software available which, given a bunch of c++11 code, derives all types of the auto-typed variables and rewrites the code with those derived types? And also for initializer lists?

The reasoning is that we would like to provide a backwards compatible version of our code (non C++11), mainly for portability with osx. Auto-typing and initializer lists are the features we use most as they make the code a lot more readable, but removing them by hand is a no-go. As this is in fact what the compiler does with auto-typed variables, it does not seem too far-fetched?

like image 961
Broes De Cat Avatar asked Feb 29 '12 10:02

Broes De Cat


2 Answers

Look at BOOST_AUTO and/or BOOST_TYPEOF

  • http://www.boost.org/doc/libs/1_48_0/doc/html/typeof/refe.html
  • http://www.boost.org/doc/libs/1_48_0/doc/html/typeof/refe.html#typeof.typo

You could substitute

 auto x = foo();

with

 BOOS_AUTO(x, foo());

If you wanted to 'manage' a decltype you'd have to resort to BOOST_TYPEOF. Note that both macros have some variants that you will want to read more about

like image 190
sehe Avatar answered Oct 23 '22 03:10

sehe


If you can use Boost, then you might look at boost::typeof. It won't do exactly what auto does, but, in most cases, it can be automatically substituted via a regex-with-captures search.

like image 29
vines Avatar answered Oct 23 '22 04:10

vines