Retrieving the type of auto in C++11 without executing the program

后端 未结 5 1504
悲&欢浪女
悲&欢浪女 2021-01-11 10:10

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 f

5条回答
  •  北海茫月
    2021-01-11 11:07

    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:

    int foo ()
    {
       auto x = bar();
       // do something with 'x'
    }
    

    Change this to:

    template  int foo_(T x)
    {
       // do something with 'x'
    }
    
    int foo ()
    {
       foo_(bar());
    }
    

    auto is specified in terms of type deduction, so the above should have very similar, if not identical semantics as the C++ '11 version.

提交回复
热议问题